简体   繁体   中英

Persisting user control values afer postback in asp.net

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM