Posted on 2010-11-09
My web application has various UpdatePanel controls, each of which contain a PlaceHolder where dynamically loaded user controls are added during runtime.
Currently I maintain the state using sessions, adding a List of Strings of the current view which are then loaded back during Postback. Various user controls have buttons which when clicked will load a further user control inside one of the other PlaceHolder controls on the Page (using findcontrol from the parent to get the PlaceHolder).
I'd like to move from Sessions to ViewState, as I have achieved row clickable GridViews which I could not fix with sessions. I have also found a number of other issues with session state management which I won't go in to here.
So, my problem is. How can I modify the Page ViewState object from a dynamically loaded UserControl. I'd like to do the following from the UserControl at runtime, i.e. when a user clicks a row of a GridView, store the index of the selected row:
Page parent = (Page)this.Parent;
parent.ViewState["controls"] = // New list of controls
The above doesn't work, and I understand that this is not possible (from various Microsoft articles). I have also tried to add a public property to the Parent Page, but this is not accessible either.