简体   繁体   English

动态加载的用户控件上丢失的Viewstate变量

[英]Viewstate variable lost on user control loaded dynamically

I have a problem with ViewState. 我的ViewState有问题。 I have an aspx page that has a treeview on the left and an UpdatePanel with an ASP.NET Panel inside on the right. 我有一个aspx页面左侧有一个树视图,右侧有一个带有ASP.NET面板的UpdatePanel。 It is in that inner Panel where I load and unload dynamically user controls. 它位于内部面板中,我可以动态加载和卸载用户控件。 I use that update panel to load dynamically controls. 我使用该更新面板加载动态控件。

I also made a custom control for my user controls because I need to pass some values from page. 我还为我的用户控件创建了一个自定义控件,因为我需要从页面传递一些值。 On that constructor I use ViewState to store these values. 在该构造函数上,我使用ViewState来存储这些值。

The first time I load the user control I call its constructor with parameters. 我第一次加载用户控件时,我用参数调用它的构造函数。 When I reload that user control on each postback I use its normal constructor. 当我在每个回发上重新加载该用户控件时,我使用它的普通构造函数。

My problem is I the values I've stored on ViewState has become null on successive postback. 我的问题是我在ViewState上存储的值在连续回发时变为null。

Update: 更新:

This is a piece of my user control: 这是我的用户控件:

public class MyUserControl : System.Web.UI.UserControl
{
private int PKId
{
    get { return ViewState["pkId"] as int; }
    set { ViewState["pkId"] = value; }
}

public MyUserControl(int pkId)
{
    this.PKId = pkId;
}

...
}

I'm following this article to load controls dynamically: http://msdn.microsoft.com/en-us/magazine/cc748662.aspx#id0070065 . 我正在按照这篇文章动态加载控件: http//msdn.microsoft.com/en-us/magazine/cc748662.aspx#id0070065

Second Update: 第二次更新:
I also set the same control ID when I load the user control at first time and on each reaload. 我在第一次加载用户控件时和每个原型上都设置了相同的控件ID。

Maybe I can use another method to store these values like input hidden fields or Cache. 也许我可以使用另一种方法来存储这些值,如输入隐藏字段或缓存。 I've choosen ViewState because I don't want to overload server with Session values for each user. 我选择了ViewState,因为我不想为每个用户重载带有Session值的服务器。

Third update: 第三次更新:

I load the controls with this code:

System.Web.UI.UserControl baseControl = LoadControl(ucUrl) as System.Web.UI.UserControl;
if (baseControl != null)
{
    baseControl.ID = "DestinationUserControl";
    PanelDestination.Controls.Add(baseControl);
}

And reaload with this code: 和reaload这段代码:

DynamicControls.CreateDestination ud = this.LoadControl(TrackedUserControl) as DynamicControls.CreateDestination;
if (ud != null)
{
    ud.ID = "DestinationUserControl";
    PanelDestination.Controls.Add(ud);
}

What's happening? 发生了什么?

When are you loading the user control? 你什么时候加载用户控件? This has to happen in the Init event if you want ViewState to be saved/restored. 如果要保存/恢复ViewState,则必须在Init事件中执行此操作。

Try storing the control into a local variable once it's loaded/constructed before it's added to the control hierarchy. 在将控件添加到控件层次结构之前,尝试将控件存入加载/构建后将其存储到局部变量中。 That allows the ViewState data to be mapped from and to the control. 这允许ViewState数据从控件映射到控件。 See "Rule 2" here http://chiragrdarji.wordpress.com/2009/05/20/maintain-viewstate-for-dynamic-controls-across-the-postback/ . 请参阅http://chiragrdarji.wordpress.com/2009/05/20/maintain-viewstate-for-dynamic-controls-across-the-postback/中的 “规则2”。

Adding controls dynamically inside an UpdatePanel is such a bad idea. 在UpdatePanel内动态添加控件是一个糟糕的主意。 It generates so much problems. 它产生了很多问题。

If it is possible, move the dynamic control creation out of the UpdatePanel and I believe your problem will be solved. 如果可能,将动态控件创建移出UpdatePanel,我相信您的问题将得到解决。

As Bryan mentioned, you should load your dynamic controls in Page_Init rather than Page_Load. 正如Bryan所提到的,你应该在Page_Init而不是Page_Load中加载动态控件。

As this description of the Page Life Cycle explains, by the time the Page_Load event happens, the view state from the postback has already been processed (in PreLoad). 正如对页面生命周期的描述所解释的那样,当Page_Load事件发生时,已经处理了回发中的视图状态(在PreLoad中)。 If the controls haven't been reloaded yet, the view state has nowhere to go. 如果尚未重新加载控件,则视图状态无处可去。

PreLoad: 预载:

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实例中包含的所有回发数据。

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

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