ASP.NET:Viewstate并以编程方式添加用户控件

时间:2021-12-01 16:50:37

When programmatically adding user controls using LoadControl(string path), when, in the user control's page life cycle, does it initialize its sub-controls with its viewstate?

当使用LoadControl(字符串路径)以编程方式添加用户控件时,在用户控件的页面生命周期中,何时使用其viewstate初始化其子控件?

I'm asking this question because one of my user controls that's being programmatically loaded has a TextBox control that is not being initialized/loaded by it's viewstate on PostBack on the Page_Load event (which is not the case for a regular .aspx pages and hence my confusion). Overall, I need to retrieve values from the Textbox control.

我问的是这个问题,因为我的一个用户控件正在以编程方式加载了一个TextBox控件,它没有被它在PageLoad事件上的PostBack上的viewstate初始化/加载(对于常规.aspx页面不是这种情况,因此我的困惑)。总的来说,我需要从Textbox控件中检索值。

Thanks

1 个解决方案

#1


11  

ViewState is loaded before the Page_Load event. If you want your control to work with ViewState, you need to load it and add it to the page before that event — usually on PreInit.

ViewState在Page_Load事件之前加载。如果您希望控件与ViewState一起使用,则需要加载它并在该事件之前将其添加到页面 - 通常在PreInit上。

The life cycle reference is here:
http://msdn.microsoft.com/en-us/library/ms178472.aspx?ppud=4

生命周期参考在这里:http://msdn.microsoft.com/en-us/library/ms178472.aspx?pp = 4

Read the description for the Pre Load event, which immediately precedes Page Load:

阅读Pre Load事件的描述,该事件紧接在Page Load之前:

Use this event if you need to perform processing on your page or control before the Load event.

如果需要在Load事件之前对页面或控件执行处理,请使用此事件。

Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

在Page实例引发此事件之前,它会为自身和所有控件加载视图状态,然后处理Request实例中包含的所有回发数据。

Thus by Pre Load time it's already too late. Also, the description for the PreInit event specifically mentions that it's the place to "create or re-create dynamic controls."

因此,通过预加载时间已经太晚了。此外,PreInit事件的描述特别提到它是“创建或重新创建动态控件”的地方。

#1


11  

ViewState is loaded before the Page_Load event. If you want your control to work with ViewState, you need to load it and add it to the page before that event — usually on PreInit.

ViewState在Page_Load事件之前加载。如果您希望控件与ViewState一起使用,则需要加载它并在该事件之前将其添加到页面 - 通常在PreInit上。

The life cycle reference is here:
http://msdn.microsoft.com/en-us/library/ms178472.aspx?ppud=4

生命周期参考在这里:http://msdn.microsoft.com/en-us/library/ms178472.aspx?pp = 4

Read the description for the Pre Load event, which immediately precedes Page Load:

阅读Pre Load事件的描述,该事件紧接在Page Load之前:

Use this event if you need to perform processing on your page or control before the Load event.

如果需要在Load事件之前对页面或控件执行处理,请使用此事件。

Before the Page instance raises this event, it loads view state for itself and all controls, and then processes any postback data included with the Request instance.

在Page实例引发此事件之前,它会为自身和所有控件加载视图状态,然后处理Request实例中包含的所有回发数据。

Thus by Pre Load time it's already too late. Also, the description for the PreInit event specifically mentions that it's the place to "create or re-create dynamic controls."

因此,通过预加载时间已经太晚了。此外,PreInit事件的描述特别提到它是“创建或重新创建动态控件”的地方。