views:

548

answers:

2

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);

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; }
}

After that, the ascx control lives a life on its own and all is well until postback occurs. When the page posts back, suddenly the view state is empty! I suspect this happens because I manipulate the ViewState before I add the instantiated ascx to my page. Also, I can prevent the ViewState from getting lost by adding the following line in the Page_Load() method of my ascx control:

SomeProperty = SomeProperty;

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.

Thanks.

A: 

Keep track of the ID of the UserControl before postback then on postback re-create the control and assign the ID back and it should automatically load the ViewState back in.

James
Oh, yeah, I forgot to say that. I actually recreate the user control on each postback, and I ensure that the ID is the same each time. The problem is not that the ViewState can't be restored, it's that changes to the viewstate between LoadControl and placeHolder.Controls.Add isn't persisted unless I explicitly repeat the changes after the control has been added to the page.
Ulrik Rasmussen
+5  A: 

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.

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).

Bob
+1 good references w/ explanation
rick schott
Thanks, that cleared it up. I'm in a dilemma now, though: I need to create some controls in Page_Init, depending on the contents of the view state (or hidden field, for that matter, which doesn't seem to get loaded in Page_Init either).
Ulrik Rasmussen
Ohh if you go with the Hidden Field approach you will need to look in `Request.Forms` for the value. The actual server control won't be loaded yet. Give that a try.
Bob
Oh, great. Nice hack :). Thanks a lot.
Ulrik Rasmussen