1

I have several instances of user controls on a page. They are also nested in each other. Since these controls are created dynamically, I am having trouble maintaining their state. I decided to save the state manually to a persistent medium (possibly Session).

On the click event on a button on the host page, I want to write functionality to save the state of the controls BEFORE the postback happens (Once there is a postback, the controls are recreated and I lose the state).

If I put this logic in the button event handler, since this is executed AFTER the postback, I dont have the information anymore.

Which method should I override?

4 Answers4

1

Try creating your button in the Page Init instead of on load.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

Not really answering you question, but I'm curious as to why the layout of your buttons is getting reset. Are you doing something via javascript?

How are you creating your buttons. in the page_load? are you checking when its a postback?

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        // load all buttons
        LoadButtons();
    }
}
bendewey
  • 39,709
  • 13
  • 100
  • 125
0

It sounds like you are re-creating the controls on postback and so they lose their state. You should make use of Page.IsPostBack to prevent this. Also check that the page allows Viewstate.

DaveC
  • 552
  • 1
  • 4
  • 20
0

Actually you want to be loading your dynamic controls in the LoadViewState method override. Don't try to maintain state yourself; it is a waste of time. You just have to put your code in the right place.

See My previous comments on this

Community
  • 1
  • 1
DancesWithBamboo
  • 4,176
  • 1
  • 19
  • 20