视图状态 - 鸡犬不宁。 [英] Viewstate - utter confusion.

查看:126
本文介绍了视图状态 - 鸡犬不宁。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有我完全摸不着头脑,任何人都可以解释一下吗?

This has me totally puzzled, can anyone explain this?

标记:

<form id="form1" runat="server">
    <asp:TextBox runat="server" ID="txtTest" />
    <asp:PlaceHolder runat="server" ID="PlaceHolder1" />
    <asp:Button runat="server" Text="Click" />
</form>

code背后:

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    txtTest.Text = "BBB";
    PlaceHolder1.Controls.Add(new TextBox() { Text = "AAA" });
}    

当我改变这两个文本框中的文字,然后点击点击按钮,在txtTest文本将恢复到原来的值'BBB'(在Page_Load中指定),但动态文本框保留我刚输入的值(而不是'AAA'),尽管这是在同一时间,另外,'硬codeD文本框中指定。

When I change the text in both textboxes, then click the 'Click' button, the text in txtTest is reverted back to the original value 'BBB'(specified in page_load), but the dynamic textbox retains the value I just entered (and not 'AAA') despite this being specified at the same time as the other, 'hardcoded' textbox.

推荐答案

有关txtTest的值被覆盖在你的Page_Load事件,所以你不会看到ViewState的值。 ViewState的值加载在preLOAD阶段。

The value for txtTest is being overwritten in your Page_Load event, so you will not see the ViewState value. The ViewState value is loaded in PreLoad stage.

有关动态控制值接收ViewState的价值,因为你设置的文本,然后添加控件到页面。当控件添加到页面上,它将发挥追赶它的事件。在这个追赶,值从ViewState中加载,覆盖您的初始值。

The value for the dynamic control receives the ViewState value because you are setting the text and then adding the control to the page. When the control is added to the page, it will play catch-up with it's events. During this catchup, the value is loaded from the ViewState, overwriting your initial value.

杰夫CYR的code不起作用,因为该控件不追赶,直到你退出的Page_Load。你可以看到这一点,如果你配合到新的文本框的Load事件,并在一对夫妇Response.Writes的抛出。

Jeff Cyr's code doesn't work because the control doesn't play catch-up until you exit Page_Load. You can see this if you tie into the new TextBox's Load event and throw in a couple of Response.Writes.

protected void Page_Load(object sender, EventArgs e)
{
    txtTest.Text = "BBB";
    //PlaceHolder1.Controls.Add(new TextBox() { Text = "AAA" });
    TextBox txt = new TextBox();
    txt.Load += new EventHandler(txt_Load);
    PlaceHolder1.Controls.Add(txt);
    Response.Write("page load");
    txt.Text = "AAA";
}

void txt_Load(object sender, EventArgs e)
{
    Response.Write("textbox load");
}

这篇关于视图状态 - 鸡犬不宁。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆