1

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 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Right on spot, one thing to add though, if you dynamically add the control in page_init method and enable viewstate for the controls, it should take care of your needs. No need to store the value explicitly in State bag.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.