简体   繁体   English

AutoPostBack:true与false

[英]AutoPostBack: true vs false

Before I begin, I have already seen this question about a very similar topic (as well as this one and this one ), none of which answer my question completely. 在开始之前,我已经看到了一个关于一个非常相似的主题 (以及这一个这一个 )的问题,其中没有一个完全回答我的问题。 I already understand the concepts presented in these questions/answers, but I have more questions. 我已经理解了这些问题/答案中提出的概念,但我还有更多问题。

A) What happens if you have multiple controls with AutoPostBack="false" and you change a number of them before a postback? A)如果您有多个AutoPostBack="false"控件并且在回发之前更改了多个控件,会发生什么? Take the following brief example (assume that everything else needed for the page is written correctly and trivially; eg, Page_Load ): 以下面的简短示例(假设页面所需的其他所有内容都是正确且简单地编写的;例如, Page_Load ):

Default.aspx: Default.aspx的:

<asp:DropDownList ID="ddlFoo" runat="server" 
    OnSelectedIndexChanged="ddlFoo_Changed" AutoPostBack="false" >
    <asp:ListItem Text="a" />
    <asp:ListItem Text="b" />
    <asp:ListItem Text="c" />
</asp:DropDownList>
<asp:DropDownList ID="ddlBar" runat="server" 
    OnSelectedIndexChanged="ddlBar_Changed" AutoPostBack="false" >
    <asp:ListItem Text="1" />
    <asp:ListItem Text="2" />
    <asp:ListItem Text="3" />
</asp:DropDownList>
<asp:Button ID="btnQux" runat="sever" Text="Click for PostBack" OnClick="btnQux_Click"

Default.aspx.cs: Default.aspx.cs:

protected void ddlFoo_Changed(object sender, EventArgs e)
{
    Response.Write("ddlFoo changed to " + ddlFoo.Text + ". ");
}
protected void ddlBar_Changed(object sender, EventArgs e)
{
    Response.Write("ddlBar changed to " + ddlBar.Text + ". ");
}
protected void btnQux_Changed(object sender, EventArgs e) { }

Now, say you change ddlFoo to 3 and then ddlBar to b . 现在,假设您将ddlFoo更改为3 ,然后将ddlBarb Then, you click btnQux . 然后,单击btnQux You get the following output from Response.Write after clicking: 单击后,您从Response.Write获得以下输出:

ddlBar changed to b. ddlFoo changed to 3. 

Why does this happen? 为什么会这样? Do the OnSelectedIndexChanged methods get put into a stack to be called once a postback happens? 一旦发生回发, OnSelectedIndexChanged方法是否会被放入堆栈中?

B) Why does my webpage load much more quickly when I use this approach and set AutoPostBack="false" for most of my controls? B)为什么我使用这种方法时我的网页加载速度要快得多,并且为我的大多数控件设置AutoPostBack="false" To be specific, I did this for a CheckBox , a DropDownList , and a TextBox in a GridView , which retrieved ~1200 rows and 27 columns of data and took 10s in VS2008 debug mode versus 310s before. 具体来说,我为GridViewCheckBoxDropDownListTextBox了这个,它检索了大约1200行和27列数据,并且在VS2008调试模式下花了10s而不是之前的310s。 Why would the load/refresh time be so much faster? 为什么加载/刷新时间会快得多?

EDIT: I released the code earlier this afternoon, and there was no significant difference between the load time of the old ( AutoPostBack="true" ) and new ( AutoPostBack="false" ) versions. 编辑:我今天早些时候发布了代码,旧版本( AutoPostBack="true" )和新版本( AutoPostBack="false" )的加载时间没有显着差异。 I think that perhaps the debugger was doing something extra, which caused the large jump in load time. 我认为也许调试器正在做一些额外的事情,这导致了加载时间的大幅增加。 A better way to rephrase question B) might be this then: What could the debugger have been doing to cause this large jump in load time? 一个更好的方法来改写问题B)可能是这样的:调试器一直在做什么来导致加载时间的大幅度增加?

Warning: I'm no ASP.NET expert... If this turns out to be garbage, I'll delete it :) 警告:我不是ASP.NET专家......如果这是垃圾,我会删除它:)

A) I believe you will see the new values of all the controls, whenever the postback ends up happening, including all the change events, just as you described. A)我相信你会看到所有控件的新值,无论何时回发结束,包括所有更改事件,就像你描述的那样。 The values have changed, after all - the AutoPostBack just affects the timing (and whether the postback occurs at all, of course). 毕竟,值已经改变 - AutoPostBack只会影响时间(当然,回发是否完全发生)。

B) There's more Javascript in the HTML delivered with AutoPostBack = True on all the controls, but not enough to make that enormous difference. B)在所有控件上使用AutoPostBack = True提供的HTML中有更多Javascript,但还不足以产生巨大差异。 As noted in your edit, it looks like that was a transient issue anyway - we can't really explain transient issues without more diagnostics. 正如您的编辑中所指出的那样,看起来这仍然是一个短暂的问题 - 如果没有更多的诊断,我们无法真正解释瞬态问题。

You can use Fiddler to see what data is moving between client and server. 您可以使用Fiddler查看客户端和服务器之间正在传输的数据。

A. With fiddler you can easily see what data is sent to the server. 使用fiddler,您可以轻松查看发送到服务器的数据。

For example: 例如:

If you have DropDownList ddlFoo , when you click on button, you actually post this information: 如果您有DropDownList ddlFoo ,当您单击按钮时,您实际发布此信息:

POST http:// [server]:[port]/[resource.aspx] HTTP/1.1 Host: [server]:[port] [Headers...] POST http:// [server]:[port] / [resource.aspx] HTTP / 1.1主机:[服务器]:[端口] [标题...]

_ VIEWSTATE[viewstate data stored in html as hidden field value]& _EVENTVALIDATION=[event validaion data]&ddlFoo=selecteItem&button1=ButtonText _ VIEWSTATE [查看存储在html中的数据作为隐藏字段值]& _EVENTVALIDATION = [event validaion data]&ddlFoo = selecteItem&button1 = ButtonText

When ASP.NET receives request, it compares ddlFoo's value and invokes it's event. 当ASP.NET收到请求时,它会比较ddlFoo的值并调用它的事件。

B. When you set AutoPostBack to true, then this javascript function will be generated: B.当您将AutoPostBack设置为true时,将生成此javascript函数:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

And onchange attribute will be added to ddlFoo . 并且onchange属性将添加到ddlFoo So whenever you change DropdownList item, onchange event will be fired and __doPostBack function will be called, which will auto post back to the server. 因此,每当您更改DropdownList项时,将触发onchange事件并调用__doPostBack函数,该函数将自动回发到服务器。

Both answers you have gotten so far are correct. 你到目前为止得到的两个答案都是正确的。 The simplified version of it is this: 它的简化版本是这样的:

A) When the form is finally POST'ed to the server, the server compares the form's current state with the ViewState and responds accordingly. A)当表单最终POST到服务器时,服务器将表单的当前状态与ViewState进行比较并相应地做出响应。

B) Enabling AutoPostBack causes javascript to be generated, and this javascript submits the form (which then triggers the postback). B)启用AutoPostBack会导致生成javascript,并且此javascript提交表单(然后触发回发)。

Why does this happen? 为什么会这样? Do the OnSelectedIndexChanged methods get put into a stack to be called once a postback happens? 一旦发生回发,OnSelectedIndexChanged方法是否会被放入堆栈中?

Events that do not immediately post back (in your case ddlFoo_Changed and ddlBar_Changed) are cached . 不会立即回发的事件(在您的情况下为ddlFoo_Changed和ddlBar_Changed)会被缓存

Then those cached/pending events are raised along with btnQux's click event, when a page is posted back (by btnQux's click event). 然后,当页面被回发(通过btnQux的单击事件)时,这些缓存/挂起事件与btnQux的click事件一起被引发。

You can read more here - ASP.NET Server Control Event Model 您可以在这里阅读更多内容 - ASP.NET服务器控件事件模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM