It is amazing to note that only two of the ASP.NET web server controls cause a postback. All the other controls use the JavaScript __doPostBack function to trigger the postback. __doPostBack function can be defined in javascript as below: function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } } The __doPostBack function takes two arguments, eventTarget and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. Note that the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from the code behind using the forms/params collection. How to find controls which caused postback??? Using the hidden variables you can also find the ID of the control which causes the postback. All you need to do is to retrieve the value of the __EVENTTARGET from the form parameter collection. string controlName = Request.Params.Get("__EVENTTARGET");
What about passing arguments??? Have a look at the __doPostBack function you will notice that the second argument is called the eventArgument. With this you can allow controls to pass arguments to the __doPostBack function. <input type="button" id="Button2" value="Button 2 Click" onclick="DoPostBack()" /> <script language="javascript" type="text/javascript"> function DoPostBack() { __doPostBack('Button2','My Argument'); } </script>
The event target can be retrieved with: string passedArgument = Request.Params.Get("__EVENTARGUMENT");
The “Button2” when clicked fires the DoPostBack function which in turn calls the __doPostBack. The __doPostBack function contains two arguments, eventTarget and eventArgument. The eventTarget is “Button2” and the eventArgument is “My Argument.”
Mind Tickler – Do you know the two controls that do not use __dopostback() are Button (submit type) and image button. They directly gets fired.
|
| Author: Mahesh Raj 07 Jun 2008 | Member Level: Gold Points : 1 |
This is very good information,Continue posting such useful articles.
|
| Author: John Fernandez 08 Jun 2008 | Member Level: Gold Points : 1 |
Very well written Article.Thanks for sharing this information.
|