简体   繁体   English

为什么即使将ASP.NET Dynamic控件添加到Page_Load中,也可以保留ViewState?

[英]Why can ASP.NET Dynamic controls keep the ViewState even when added in Page_Load?

I have done a bit of research related to dynamic controls and ViewState. 我做了一些有关动态控件和ViewState的研究。

And I read that in order to keep the ViewState for a dynamic control you have to add it in the Page_Init event. 我读到,为了使ViewState保留为动态控件,您必须将其添加到Page_Init事件中。 It makes sense because the PageLifeCycle is : 这是有道理的,因为PageLifeCycle是:

  1. Initialization. 初始化。
  2. LoadViewState. LoadViewState。
  3. LoadPostbackData. LoadPostbackData。
  4. Load. 加载。
  5. RaisePostbackEvent. RaisePostbackEvent。
  6. SaveViewState. SaveViewState。
  7. Render. 渲染。

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. 但是我做了一个测试应用程序,我看到即使将控件添加到Page_Load事件中(而不是之后),也将保留ViewState和properties值。 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. 有人说控件可能会赶上PageLifeCycle,而有人说必须将它们添加到Page_Init中。 Can someone clarify this for me? 有人可以帮我澄清一下吗?

Also in msdn I found: 另外在msdn中,我发现:

Note You may be able to get away with loading your controls in the Page_Load event handler and maintaining the view state properly. 注意您可以通过将控件加载到Page_Load事件处理程序中并正确维护视图状态来摆脱困境。 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. 这完全取决于您是否以编程方式设置动态加载控件的任何属性,如果是,则相对于Controls.Add(dynamicControl)行进行设置。 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. 对此进行详尽的讨论超出了本文的范围,但之所以可行,是因为Controls属性的Add()方法将父级的视图状态递归地加载到其子级中,即使加载视图状态阶段已经过去。

But I don't really understand this fully so I hope someone can explain it. 但是我对此并不十分了解,所以我希望有人能解释一下。 Thank you in advance. 先感谢您。

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). 这是发生了什么 -究竟为什么它就像那是另一个问题(它实际上是什么,我会想到阅读文档相反)。

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. 我忘了提到-本质上这是由于以下事实:当控件通过Controls.Add()添加到控件树中时,控件会在页面生命周期中播放以“赶上”该页面-对此有无数篇文章,因为它没有很多直接的东西。

In the past (ASP.NET 2.0 or 3.5, not sure), when trying to implement the same as you are mentioning, I had to add the controls in the Page_Init. 过去(不确定ASP.NET 2.0或3.5),当尝试实现与您提到的相同时,我不得不在Page_Init中添加控件。 Adding them in the Page_Load I wouldn't see the changes made in the client side arrive to the server side, which makes perfect sense because when the framework was trying to bind the viewstate to the controls, they were not created yet. 将它们添加到Page_Load中时,我看不到客户端所做的更改会到达服务器端,这非常有意义,因为当框架尝试将视图状态绑定到控件时,尚未创建它们。

I'm very surprised to know this is changed. 得知这一变化,我感到非常惊讶。 Maybe something introduced in ASP.NET 4.0? 也许在ASP.NET 4.0中引入了一些东西?

在NET 4.5中,只需重写CreateChildControls()方法并将其放置在动态控件内部即可。

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

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