问题
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();
}
}
回答1:
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!
回答2:
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
回答3:
Well, the reason that you are having doubt is because you think function __doPostBack is needed to post. But it is other way round, when html was first designed, only way to post back to server was via submit button. This functionality was build in the browser them self, such that when ever the submit button was pressed browser passed the data of the form to the location declared. but as the web developed people started demanding for more. Now other controls also needed to post back to server. To implement them various hacks was developed, the most well known __doPostBack function is one of them. any control other that submit now can post back using __doPostBack function. But submit button do that themself. Thats all.
来源:https://stackoverflow.com/questions/15678481/how-does-the-dopostback-method-get-called-where-is-the-calling-method