ASP.NET: Viewstate and programmatically adding user controls(ASP.NET:Viewstate 和以编程方式添加用户控件)
问题描述
当使用 LoadControl(string path) 以编程方式添加用户控件时,在用户控件的页面生命周期中,它是否使用其视图状态初始化其子控件?
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?
我问这个问题是因为我的一个正在以编程方式加载的用户控件有一个 TextBox 控件,该控件没有被 Page_Load 事件的 PostBack 上的视图状态初始化/加载(常规 .aspx 不是这种情况)页面,因此我感到困惑).总的来说,我需要从 Textbox 控件中检索值.
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.
谢谢
推荐答案
ViewState 在Page_Load 事件之前加载.如果您希望您的控件与 ViewState 一起使用,您需要在该事件之前加载它并将其添加到页面中 - 通常在 PreInit 上.
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.
生命周期参考在这里:
http://msdn.microsoft.com/en-us/library/ms178472.aspx?ppud=4
阅读 Pre Load
事件的描述,该事件紧接在页面加载之前:
Read the description for the Pre Load
event, which immediately precedes Page Load:
如果您需要在加载事件之前对页面或控件执行处理,请使用此事件.
Use this event if you need to perform processing on your page or control before the Load event.
在 Page 实例引发此事件之前,它会为自身和所有控件加载视图状态,然后处理 Request 实例中包含的所有回发数据.
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.
因此到预加载时间已经太晚了.此外,PreInit 事件的描述特别提到它是创建或重新创建动态控件"的地方.
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."
这篇关于ASP.NET:Viewstate 和以编程方式添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET:Viewstate 和以编程方式添加用户控件


- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01