如何在__doPostBack方法被调用?哪里调用的方法? [英] how does the __doPostBack method get called? Where is the calling method?
问题描述
我用了一个< ASP:按钮/>
控制,并在控制不具有单击浏览器在渲染之后
事件属性分配。它究竟是如何调用SEVER端事件?
ASPX code:
< ASP:按钮的ID =Button1的=服务器文本=按钮的OnClick =TestClickEvent/>
以上控制在浏览器中呈现如下code:
<输入类型=提交名称=Button1的值=按钮ID =Button1的>
以下code在浏览器中,这台 __ EVENTTARGET
渲染。 我的疑问是如何在 __ doPostBack
方法被调用?哪里调用方法?
函数__doPostBack(的eventTarget,eventArgument){
如果(!theForm.onsubmit ||(theForm.onsubmit()!= FALSE)){
theForm .__ EVENTTARGET.value =的eventTarget;
theForm .__ EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
答案很简单:的 __ doPostBack
基于特定的JavaScript函数调用< ASP />
控制和的它处理的事件
详细的答案:这取决于
首先,让我们捂住例如您有一个< ASP:按钮/>
它呈现为一个标准的<输入类型=提交/>
。一切都在 ASP.NET的WebForms 围绕标准的 HTML <形式为GT;
标记。一个HTML <形式>
通过点击提交的没有 JavaScript的使用或援助的 <输入类型=提交/方式>
按钮
考虑到这一点,你可以很好地看到(你已经注意到)的渲染<输入类型=提交/>
按钮的功能不可以有一个的onclick
事件分配。而且,正如你所看到的,单击该按钮时提交表单。
当谈到如何后端(C#/ VB.NET /等)被执行code当<输入类型=提交/>
按钮被点击:它是所有的ASP.NET框架本身处理,超出了这个问题/答复的范围
二,现在让我们来讨论什么是 __ doPostBack
是,以及它是如何使用的。 __ doPostBack
简直就是JavaScript函数用来提交HTML ℃的帮手;形式>
。由于上述原因,你现在知道为什么<输入类型=提交/>
按钮做的不可以需要调用 __ doPostBack
功能。
为了简单起见,让我们来看看它有一个℃的ASP.NET页面; ASP:DropDownList中/>
控制,它具有的SelectedIndexChanged
事件处理程序分配的:
< ASP:DropDownList的ID =MyDropDownList的AutoPostBack =真OnSelectedIndexChanged =MyDropDownList_SelectedIndexChanged=服务器 />
的< ASP:DropDownList中/>
呈现如下:
<选择一个id =ctl00_MyDropDownList的onchange =JavaScript的:的setTimeout('__ doPostBack(\\'ctl00 $ MyDropDownList \\',\\'\\')',0)NAME = ctl00 $ MyDropDownList>< /选择>
的让我们忽略在的onchange
事件的setTimeout
功能 - 它仅仅是一个哈克解决方法使用ASP.NET - 让我们把重点放在 __ doPostBack
函数里面它的
正如你所看到的,在 __ doPostBack
函数的是的正在由的onchange
事件处理程序。关键的区别是,改变一个&LT的价值; ASP:DropDownList中/>
或<选择/>
控制不会导致浏览器提交表单!
再次ASP.NET框架内部处理时如何提交表单后端code执行(无论是通过 __ doPostBack
功能与否)。
最后,至于的__ doPostBack细节
:它接受两个参数 - 的eventTarget
和 eventArgument
。 的eventTarget
包含导致回发的控件呈现的HTML ID
财产;和 eventArgument
是一个可以用来传递额外的数据到后端code可选参数。
修改附加信息:的OP问了一个很有趣的问题 - 当有不止一个提交按钮会发生什么
?好了, POST
操作过程中,浏览器包括值
的&LT的;输入类型=提交/方式>
这引起了操作启动
这意味着,就像你得到的价值你的&LT;输入/&GT; <!/ code>元素,你也可以查询哪个按钮引起了提交
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();
}
}
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!
这篇关于如何在__doPostBack方法被调用?哪里调用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!