ASP.NET动态控制为什么能保持在Page_Load中添加即使在ViewState的? [英] Why can ASP.NET Dynamic controls keep the ViewState even when added in Page_Load?

查看:105
本文介绍了ASP.NET动态控制为什么能保持在Page_Load中添加即使在ViewState的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些调查有关动态控件和ViewState中。

I have done a bit of research related to dynamic controls and ViewState.

和我读到为了保持ViewState中的动态控制,你必须将它添加在Page_Init事件。这是有道理的,因为PageLifeCycle是:

And I read that in order to keep the ViewState for a dynamic control you have to add it in the Page_Init event. It makes sense because the PageLifeCycle is :


  1. 初始化。

  2. LoadViewState。

  3. LoadPostbackData。

  4. 加载。

  5. RaisePostbackEvent。

  6. SaveViewState。

  7. 渲染。

不过,我做了一个测试应用程序,我看到的ViewState和属性值是preserved即使我在Page_Load事件中添加控制,而不是之后。从这个对我才发现矛盾,情报。有人说,控制可能赶上其他PageLifeCycle说你必须在Page_Init添加它们。有人可以澄清这对我?

But I made a test app and I saw that the ViewState and properties values are preserved even if I add the control in the Page_Load event and not after. From this on I only found contradictory informaton. Some say that the controls may catch up the PageLifeCycle other say you must add them in the Page_Init. Can someone clarify this for me?

此外,在MSDN,我发现:

Also in msdn I found:

请注意您可以在加载您的控件脱身
  Page_Load事件处理程序,妥善维护视图状态。它
  一切都取决于你是否正在设置的任何属性
  动态加载控件编程方式,如果是这样,当你
  这样做相对于Controls.Add被(dynamicControl)线。彻底
  这种讨论是有点超出了本文的范围,但
  原因它可能工作是因为Controls属性的Add()方法
  递归加载父视图状态到它的孩子,甚至
  尽管负荷视图状态的阶段已经过去了。

Note You may be able to get away with loading your controls in the Page_Load event handler and maintaining the view state properly. It all depends on whether or not you are setting any properties of the dynamically loaded controls programmatically and, if so, when you're doing it relative to the Controls.Add(dynamicControl) line. A thorough discussion of this is a bit beyond the scope of this article, but the reason it may work is because the Controls property's Add() method recursively loads the parent's view state into its children, even though the load view state stage has passed.

但我真的不完全理解这个,所以我希望有人能解释它。谢谢你在前进。

But I don't really understand this fully so I hope someone can explain it. Thank you in advance.

推荐答案

这code将证明它在行动:

This code will demonstrate it in action:

protected void Page_Load(object sender, EventArgs e)
{
    Button b1 = new Button();
    Button b2 = new Button();
    if (!IsPostBack)
    {
        b1.Text = "Button1";
    }
    this.form1.Controls.Add(b1);
    this.form1.Controls.Add(b2);
    if (!IsPostBack)
    {
        b2.Text = "Button2";
    }
}

因此​​,如果您修改控制它被添加到它保持其视图状态后的形式,但是如果你修改它,你将它添加到窗体之前的文本不使其进入视图状态。这是的什么的情况 - 确切的为什么的它就像那是另外的问题(它实际上是什么,我会想到阅读文档相反)

so if you modify the control after it is added to the form it keeps its viewstate, but if you modify it before you add it to the form the text doesn't make it into the viewstate. This is what happens - exactly why it is like that is another questions (it is actually the reverse of what I would have thought reading the docs).

修改结果
我忘了提 - 本质上,这是由于这样的事实,通过页面生命周期控制剧目赶上,当它通过Controls.Add被()添加到控制树的页面 - 有关于这一点,因为无休止的文章没有很多关于它是简单的。

EDIT
I forgot to mention - essentially this is due to the fact that the control plays through the page lifecycle to "catch up" with the page when it is added to the control tree through Controls.Add() - there are endless articles on this because there isn't a lot about it that is straightforward.

这篇关于ASP.NET动态控制为什么能保持在Page_Load中添加即使在ViewState的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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