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!