ASCX的视图状态回发之间丢失 [英] Viewstate of ascx lost between postbacks

查看:196
本文介绍了ASCX的视图状态回发之间丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET应用程序中,我加载动态使用LoadControl,采用以下模式的的.ascx:

In my ASP.NET application, I am loading an .ascx dynamically using LoadControl, using the following pattern:

var ctrl = LoadControl("/path/to/control.ascx");
((ControlType)ctrl).SomeProperty = someData;
placeholder.Controls.Add(ctrl);

这是我添加的控制会直接保存SomeProperty属性值的ViewState,如下:

The control that I add saves the SomeProperty property value directly to ViewState, as follows:

public int? SomeProperty
{
    get { return (int?)ViewState["SomeProperty"]; }
    set { ViewState["SomeProperty"] = value; }
}

在这之后,ascx控件住在自己的生命,一切都很好,直到回发发生。当页面回,突然视图状态是空的!我怀疑这是因为我操作的ViewState之前,我实例化ASCX添加到我的网页。另外,我可以prevent ViewState中,从加入我的ascx控件的的Page_Load()方法如下行迷路:

SomeProperty = SomeProperty;

我必须做的上述每一个属性,以确保ViewState是preserved。现在,有这样做的prettier方式?操作ViewState中后实例化的.ascx已添加到页面是不是一种选择 - 我需要ViewState中的内容,在 Page_Init()的Page_Load()方法,其触发的一瞬间,我的的.ascx添加到我的网页。

I have to do the above for each and every property to ensure that the ViewState is preserved. Now, is there a prettier way of doing this? Manipulating the ViewState after the instantiated .ascx has been added to the page is not an option - I need the contents of the ViewState in the Page_Init() and Page_Load() methods, which are triggered the instant I add the .ascx to my page.

感谢。

推荐答案

看看在 ASP。 NET页生命周期了解视图状态。视图状态被初始化加载后,所以你将不能访问它在 Page_Init 。你会更好使用隐藏域。

Take a look at the ASP.NET Page Life Cycle and Understanding View State. View State gets loaded after Initialization, so you won't be able to access it in Page_Init. You'd be better off using a hidden field.

如果你是使用视图状态死心塌地,最早可以得到它会通过覆盖 LoadViewState 办法(别忘了拨打 base.LoadViewState 尝试虽然访问)之前。

If you are dead set on using View State, the earliest you can get to it would be by overriding the LoadViewState method (Remember to call base.LoadViewState before trying to access it though).

这篇关于ASCX的视图状态回发之间丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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