For one of my requirement, I need to implement this functionality on how to call server side event in JQuery. There are many scenarios on why we need to implement it or what is the need for it. I have a button and when user click on it, I need to show a client side light box which exactly functioning as window confirm box. If user selects OK, then I need to do some client side validations and if everything passed, then i need to make a call to server to execute the actual server side click event. I think this is general scenario. There are many cases other than this. So, here is a simple solution on how to call server side event in JQuery. Enjoy this interesting blog post.
1. Create a protected page variable in Server side ASPX.cs file as shown below.
protected string serversideEvent = string.Empty;
2. In Page_Load event, set the serversideEvent variable to the button click event as below.
serversideEvent = Page.ClientScript.GetPostBackEventReference(btnSubmit, string.Empty);
3. In your JQuery, use this function to evaluate on specific condition as shown in below line.
eval(<%=serversideEvent %>);
"btnSubmit" is the ID of the control <asp:Button in ASPX page. and GetPostBackEventReference will get the click event of the related button and return it in the form of string.
That's it. Whenever the line mentioned in 3rd step called, it will call server side event and execute it.
How awesome it is? Very simple and nice way to do it. Isn’t a good and valuable find?
0 komentar:
Post a Comment