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

ASP.NET: Viewstate and programmatically adding user controls(ASP.NET:Viewstate 和以编程方式添加用户控件)
本文介绍了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 和以编程方式添加用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)