Here we are going to talk about __doPostBack function.
You can see this __doPostBack function in your asp.net generated HTML code, when you do a "View Source" in the browser
Other than Submit button, all other asp.Net controls use this function to do the postback
Below is how the __doPostBack function looks like in your ASP.NEt generated HTML code
< input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" /> < input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /> function __doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) { theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument; theForm.submit(); } }
The 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.
In any asp.net page 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 or params collection.
The ID of the control which causes the postback is stored in the __EVENTTARGET hidden field, so you can find the control which caused the postback.
protected void Page_Load(object sender, EventArgs e) { string controlId1 = Request.Params.Get("__EVENTTARGET"); //OR string controlId2 = Request.Forms.Get("__EVENTTARGET"); }
Let us see how this function works for a linkbutton. When you drag a LinkButton from toolbox to webform,
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton> The generated code after rendering generated HTML
<a id="LinkButton1" href="javascript:__doPostBack('LinkButton1','')">LinkButton</a>
You can see the fucntion call "__doPostBack('LinkButton1','')" in href and the argument passed for eventTarget is "LinkButton1" which is the id of the linkbutton control (EventSource)
You can also make a custom call to __doPostBack function and pass id as target and the event information as argument.
Happy Coding !!!
|
No responses found. Be the first to respond and make money from revenue sharing program.
|