1
votes

I have a few user controls which I add to the aspx form depending on the user's choice from a combo box. I have a user control which has a textbox in it and a getValue() method that returns the value of the textbox.

After user selects the related item I load the control and add to a panel using loadControl method. User enters some text. After a postback I want to keep the user control and the user input in the same state before .

Hope this is clear.

2

2 Answers

3
votes

There is going to be a two step process.

  1. Ensure that you are adding the control to the page on the Page_Init method
  2. You can then use ViewState (ViewState["MyKey"] = "My Value";) to store and retrieve the values.

The key here being that you MUST add the control in Init so that viewstate can be loaded.

2
votes

You can add whatever you like to the page state using ViewState.

ViewState["myvalue"] = "?";

In this way you can inspect previous values on future postbacks like so:

someVariable = ViewState["myvalue"];

Even better, you can encapsulate this in a property on your page.