Most of us might have wanted to share a client side return value with the server side code. Say, for example, I want to get the user's confirmation on whether to save the existing data and move to the next page or not?
I say, yes, it is possible, but how, we will see in the sections below.
Client and Server Interaction
To pass any value to the client, we can easily do by passing the value as an argument to the client side script function.
Like, for the
OnClick event of the
btnShowMonths button, I've passed the
target attribute as the argument to my
ShowMonths function of the client side script.
private void Page_Load(object sender, System.EventArgs e)
Passing a value to the client side is a fairly easy task, but the issue is how to get the response back from the client to the server side code. Like, in our code, if the user confirms to show months, my code should fill the list box with the values of all the months, else it should be filled with the days starting from "01" - "31".
No doubt, AJAX is a technique we could have used, but for fairly simple tasks like this, it would be an overhead.
So to avoid the overhead, we can handle the things as described in this article.
To get the client return, we can use the
After post back, these two values are automatically stored in two hidden boxes of the ASPX page, by default.
"__EVENTARGUMENT". These two values can be fetched using
Request.Form["__EVENTARGUMENT"] respectively, as we have done in the code below:
private string EventArgs
private string EventTarget
Handlepostback method in the code actually checks the value of the
EventTarget after each post back to check which server side function or method to execute.
If our client side code posts back some other target methods, we can add them to our list here in the
[Note: this will only work if at least one control on our web page has the
AutoPostBack property set to
__doPostBack not found.]