2

I have a UserControlParent that dynamically loads UserControlChild.

In the UserControlChild, I store a value using the ViewState object by doing a

ViewState["count"] = myCount;

On Postbacks, the ViewState returns a null. Is this because the UserControlChild is being loaded dynamically? If it helps, the UserControlParent is loaded dynamically in the ASPX page as well.

4 Answers4

0

Have you tried creating debugging this in your UserControl?

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
}
bendewey
  • 39,709
  • 13
  • 100
  • 125
0

Is ViewState enabled for your application? There are several places where it could be shut off namely in the web.config, the page itself, UserControlParent, or UserControlChild.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • Yes, viewstate is enabled on the page and in the controls –  Feb 06 '09 at 16:29
  • What about in the web.config? – Andrew Hare Feb 06 '09 at 17:08
  • Same in web.config as well. I think this issue has to do something with page life cycle and dynamically adding user controls. I don't know the exact problem though. –  Feb 06 '09 at 17:47
  • At what point in the life cycle does "ViewState["count"] = myCount;" occur? – Andrew Hare Feb 06 '09 at 17:50
  • I assign the value after Page_Load –  Feb 06 '09 at 19:05
  • At what point are you reading the value from the view state? Also, at exactly which stage are you adding the value to the viewstate? You mentioned after Page_Load, but in what event? – Phaedrus Feb 06 '09 at 21:36
0

There are a few possible causes for this.

First, ensure that you specify the ID of the UserControlParent, the UserControlChild, and every runat="server" control in their parent hierarchy. If you miss one, ASP.NET will automatically assign one. This ID can (under certain circumstances) be different before and after your postback, thus breaking the ViewState.

Second, ensure that EnableViewState="true" for every control in the parent hierarchy, right the way up to the page. If any of them is false, it will turn off the ViewState for all its children too.

Third, as you are dynamically creating your UserControlChild, make sure you create it (and add it to UserControlParent.Controls) early enough in the page lifecycle. If you do it after the page has loaded the ViewState, you'll just get nulls back from the ViewState. Adding it in Page.Init is early enough; in Page.Load is too late.

Hope this helps!

teedyay
  • 23,293
  • 19
  • 66
  • 73
  • I had a similar problem, as stated [here](http://stackoverflow.com/a/23143377/213871), but in my code I can also use the **containing** page `Load` event to access the children ViewState. – ceztko Apr 17 '14 at 21:22
0

teedyay,

I have all IDs specified (except for GridView rows), and view state is enabled everywhere, and controls are added statically in the markup. I still don't get the ViewState saved across post backs - thinking it's a bug in asp.net.

Here's the tree of controls:

Master; Page - Ajax:TabContainer - Ajax:TabPanel - UC1 - UC2 - Gridview - its rows

Looks like nested user controls do not persist view state. I had the same problem on a different project and the only solution i fould was to avoid nesting user controls.

If anyone knows the reason behind this behavior, please let know.

Thanks.