Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the __doPostBack method get called? Where is the calling method?

I used an <asp:Button /> control, and after rendering in the browser that control doesn't have a click event property assigned. How exactly is it calling the sever side event?

ASPX code:

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="TestClickEvent" />

The above control was rendered in browser as following code:

<input type="submit" name="Button1" value="Button" id="Button1">

The following code is rendered in the browser, which sets __EVENTTARGET. My doubt is how does the __doPostBack method get called? Where is the calling method?

 function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
        }
    }
like image 681
Prabhakaran Parthipan Avatar asked Mar 28 '13 09:03

Prabhakaran Parthipan


People also ask

What does JavaScript __ Dopostback mean?

Understanding the JavaScript __doPostBack Function. This method is used to submit (post back) a form to the server and allows ASP.NET framework to call appropriate event handlers attached to the control that raised the post back.

What does PostBack mean?

In web development, a postback is an HTTP POST to the same page that the form is on. In other words, the contents of the form are POSTed back to the same URL as the form. Postbacks are commonly seen in edit forms, where the user introduces information in a form and hits "save" or "submit", causing a postback.

Does ASP.NET do PostBack?

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing. PostBack is the name given to the process of submitting an ASP.NET page to the server for processing.


2 Answers

The simple answer: The __doPostBack JavaScript function is called based on specific <asp /> controls and the events that it handles.

The detailed answer: It depends.


First, let's cover your example. You have an <asp:Button /> which is rendered as a standard <input type="submit" />. Everything in ASP.NET WebForms revolves around the standard HTML <form> tag. An HTML <form> is submitted without the use or assistance of JavaScript through clicking an <input type="submit" /> button.

With this in mind, you can very well see (which you've already noticed) that the rendered <input type="submit" /> button does not have an onclick event assigned. And, as you can see, the form is submitted when the button is clicked.

When it comes to how the back end (C#/VB.NET/etc.) code is executed when the <input type="submit" /> button is clicked: it is all handled by the ASP.NET Framework itself, and is beyond the scope of this question/answer.


Second, now let's cover what __doPostBack is, and how it is used. __doPostBack is simply a helper JavaScript function used to submit the HTML <form>. Due to the reasons outlined above, you now know why the <input type="submit" /> button does not need to call the __doPostBack function.

For simplicity, let's take a look at an ASP.NET page which has an <asp:DropDownList /> control, and it has the SelectedIndexChanged event handler assigned:

<asp:DropDownList ID="MyDropDownList" AutoPostBack="true" OnSelectedIndexChanged="MyDropDownList_SelectedIndexChanged" runat="server" />

The <asp:DropDownList /> is rendered as follows:

<select id="ctl00_MyDropDownList" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MyDropDownList\',\'\')', 0)" name="ctl00$MyDropDownList"></select>

let's ignore the setTimeout function in the onchange event - it's merely a hacky workaround used by ASP.NET - and let's focus on the __doPostBack function inside of it.

As you can see here, the __doPostBack function is being called by the onchange event handler. The key difference is that changing the value of an <asp:DropDownList /> or <select /> control does not cause the browser to submit the form!

Once again the ASP.NET Framework handles internally how the back end code is executed when the form is submitted (whether through the __doPostBack function or not).


Lastly, as for the details of __doPostBack: it accepts two parameters - eventTarget and eventArgument. eventTarget contains the rendered HTML id property of the control which is causing the postback; and eventArgument is an optional parameter which can be used to pass additional data to the back end code.


Edit Additional Info: the OP asked a very interesting question - what happens when there is more than one submit button?

Well, during a POST operation, browsers include the value of the <input type="submit" /> which caused the operation to initiate.

This means, that just as you obtain the values of your <input /> elements, you can also query for which button caused the submit!

like image 54
Jesse Avatar answered Sep 23 '22 12:09

Jesse


Your control is a submit button, it can directly submit the form to the server without calling the __dopostback method. So in this case __dopostback is not getting called. __dopostback is used for controls not having a default submit behaviour, like dropdownlist.

The wireing to your server side event happens through the ipostbackeventhandler interface that your control implements. For more info please check out this http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx

like image 37
Atti Avatar answered Sep 21 '22 12:09

Atti