ICallbackEventHandler - How to invoke a Server side code from client side script without postback in ASP.NET?
I had received above questions from my team mate, hence I thought exploring and writing a post about Call back Event.for our reference.
Now, the actual requirement is a set in the business logic (ASP.NET code) should be executed without page post back from server-side scripting. So, how client callbacks can be achieved within Server-side scripts? Yes, the answer is using ICallbackEventHandler interface with ASP.NET and JQuery scripts. Let’s understand the technique with code snippets, a simple example is taken here for explanation for ease of learning.
1. Let’s start with Server side code changes, The ICallbackEventHandler interface is inherited to implement the RaiseCallbackEvent and GetCallbackResult methods.
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{}
=========================================================
Advertisement: Choosing .NET Core Worker Services OR Windows Services?
=========================================================
Advertisement: Choosing .NET Core Worker Services OR Windows Services?
=========================================================
2. Create a reference to the CallbackEvent
string callBackRef = Page.ClientScript.GetCallbackEventReference(this, "parameter", "ReturnStatus", "");
Note: 2nd argument "parameter" is parameter name, can be named as anything. 3rd argument "ReturnStatus" is a function name, explained at step 5 below.
3. Register the Client side script to invoke Server side code
string callBackScript = "function ClientSideMethod(parameter,context)" + "{" + callBackRef + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"",callBackScript,true);
Note: function name ClientSideMethod should be mentioned in client script, on click event.
4. Next thing, Implement the RaiseCallbackEvent and GetCallbackResult methods
public string GetCallbackResult()
{
return "Status from Callback method";
//This text is received at client script
}
public void RaiseCallbackEvent(string eventArgument)
{
System.Diagnostics.EventLog.WriteEntry("Application", "Serverside script is executed");
//the expected bussines logic is implemented here, this code snippet writes into eventvwr as simple example.
}
5. Finally, Client side changes, Now invoke the Server side code on button click.
<div>
<input type="button" value="Invoke Server Method" onclick="ClientSideMethod('parmeter')"/>
</div>
<script type="text/javascript">
//Client Call back, This method must be implemented.
function ReturnStatus(result, context)
{
alert('Called' + result);
}
</script>
That’s it. Now you can run the application and click the Invoke button, a new log entry would be created without page postback. For detailed explanation for every step Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages
Happy Learning!
Comments
clear their motiѵe, and that іs also happenіng with this ρaragraph whiсh I am reаding at this time.
Also see my page > Angry Rants
thiѕ topic to be асtually somethіng that
Ι think I would neνer understand. It seеms too complіcatеd and extremely broаd for mе.
I am looking fоrwaгd for уour next роѕt, I ωill try to gеt the hang of it!
Mу webpage :: 02 broadband
get home. I'm amazed at how fast your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .
. Anyhow, awesome blog!
Have a look at my web site; Storage Dayton
is really pleasant.
Have a look at my webpage ... Best online backup providers
done an impressive job and our entire community will be thankful to you.
Check out my web-site Best online backup providers
I really like all of the points you made.
My homepage; Green Coffee bean extract for weight loss
I really like all of the points you made.
Have a look at my homepage :: Green Coffee bean extract for weight loss
to find a theme or plugin that might be able to correct this problem.
If you have any recommendations, please share. With thanks!
my page :: http://Www.youtube.com/watch?V=ahhfque6Akw
seemed to be on the web the easiest thing to be aware of.
I say to you, I certainly get annoyed whilst other folks consider
worries that they plainly don't recognise about. You controlled to hit the nail upon the top and outlined out the entire thing with no need side effect , other people could take a signal. Will likely be again to get more. Thank you
Check out my weblog: Green coffee bean extract for weight loss
had to ask. Does managing a well-established blog
like yours require a large amount of work? I'm brand new to writing a blog however I do write in my journal on a daily basis. I'd
like to start a blog so I can share my experience
and feelings online. Please let me know if you
have any ideas or tips for brand new aspiring bloggers.
Appreciate it!
http://sites4students.com/index.php?a=stats&u=barryvickers
my blog post; http://corexins.com
Feel free to visit my website: TC-L47E50 Reviews
my web-site; Acid Reflux remedies
excellent info you have got right here on this post.
I will be coming back to your web site for more soon.
Here is my website ; File Sharing Online
certain medications. Aloe VeraThis magical plants provides many benefits to
our health, one of them is to improve blood circulation in the
scalp from the tight hairstyle.
Feel free to visit my blog post hair loss in women
and the Food and Drug Administration recognizes that omega three
fatty acids may help prevent heart disease. The problem is that
the primary source of glutamine during exercise is from broken down muscle tissue; the body actually breaks down muscle tissue.
Also visit my webpage ... jacked 3d
make sure that you can find the one that makes you walk in comfort.
I believe that the Hi-Tec Midland hiking boots for men that
Campmor. However, no need to panic if you are an animal rights activist as
most fashion houses used synthetic fur for their collections.
my web page; waterproof hiking boots australia
are in fact awesome for people knowledge, well, keep up the nice work fellows.
Also visit my web page bet angel
their actions. Don't beat yourself up for not doing
this perfectly. He will not be able to attend their
daughter's high school graduation.
Stop by my blog go to webѕite
I ԝonder how much attempt you plaсе to make this kind оf ɡreat infcormative website.
Τake a look at my web pаge xtremeantler.org.uk ()
is hеlpful in favor of my know-ɦow. thanks admin
My website - Tablet PC 2LooK Quad Core CPU the best performance 4GB RAM smartphone android mobitel
authored myself or outsourced but it seems a lot of
it is popping it up all over the web without my agreement.
Do you know any ways to help stop content from
being ripped off? I'd really appreciate it.
Here is my blog; Christian Louboutin Heels
with Searсh Engine Optimization? I'm trying to
get my blog to rank for some targeted keywords but І'm not seeing
very goоd results. If you know of any pleаse share. Thank you!
Feel free to surf to my weƅ blog: Tablet PC 2LooK Quad Core CPU the best performance 4GB RAM smartphone android mobitel
you maҟe blogging look easy. Ƭhe overall look of your site is fantastic, let ɑlone the content!
Here is my web blog; Tablet PC 2LooK Quad Core CPU the best performance 4GB RAM smartphone android mobitel
Also, thank you for allowing me to comment!
Here is my web blog :: magento themes
They're very convincing and can certainly work. Nonetheless, the posts are
very short for novices. Could you please lengthen them a bit
from next time? Thank you for the post.
Also visit my web blog; Christian Louboutin Sale