How can I save asp:HiddenField value across postback?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This has nothing to do with ViewState. A form control’s value is maintained by doing a POST. As long as the control is created early enough in the page lifecycle, the posted value will be set on the control. If you refresh the page or click on a hyperlink that does a GET, then the value will be lost or revert to the designer-generated default.
Back to your question, if you have a designer-generated
HiddenField(in the aspx file), it should automatically set the value on postback. Either you’re changing it somewhere else in your code or you are trying to access the value before it has been set (i.e. beforePage_Load()). If you have a code-generatedHiddenField, it needs to have the same ID and be created before the Page sets the posted values, such as inOnInit.I would recommend you read through and understand the following articles. Otherwise, you will keep hitting walls because the Page lifecycle and ViewState are fundamental.
http://msdn.microsoft.com/en-us/library/ms972976.aspx
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx