http://msdn2.microsoft.com/en-us/library/ms972976.aspx
http://www.codeproject.com/aspnet/ViewState.asp
Myth:
Most ASP.NET developers think that the ASP.NET ViewState is responsible for holding the values of controls such as TextBoxes, DropDown, CheckBox and other Web Controls, across postback, so that they are retained even after postback. But this is nothing more than a Myth.
The ViewState is not responsible for storing the modified values/postback data for controls such as TextBoxes, dropdowns, CheckBoxList etc.
The hidden actor is the ‘IPostBackDataHandler’ interface. i.e For those controls that inherits ‘IPostBackDataHandler’ interface, postback data is not required to be dependent on the View State [The Myth].
Note: Label control does not inherit from the IPostBackDataHandler interface, so the value of the Label control does not persist if the ‘EnableViewState’ flag is set to false.
Lets us c with the help of events to know how it happens.
þ Page_Init event occurs:
þ Here the Page and controls fire their Init events
þ At this point in the page life cycle, the control hierarchy has been constructed, and the Web control properties that are specified in the declarative syntax have been assigned.
þ LoadViewState event occurs:
þ This stage only happens when the page has been posted back.
þ During this stage, the view state data that had been saved from the previous page visit is loaded and recursively populated into the control hierarchy of the Page. But if the View state is disabled then nothing happens at this stage.
þ LoadPostBackData event:
þ This stage only happens when the page has been posted back.
þ Here the Page class loads the values of those controls which inherit from the IPostBackDataHandler interface, (e.g., TextBox, Dropdowns, Checkboxes and other), by calling the control's LoadPostData()
method.
þ It works well even when the ViewState is disabled.
Note: It is a common misconception among developers that view state is somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. This is not the case, as the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.
Validate the Myth using below Test Case:
Create an example having below. [Note: Set EnableViewState = false for all controls declared below].
- Textbox [ Declarative value: ‘Textbox1’]
- Label [ Declarative value: ‘Lable1’]
- Button [Caption; ‘Change’]
- With below code behind
- TextBox1.Text = "Change 1";
- Label1.Text = "Change ";
- Button [Caption; ‘Post back] :
- This is simply used to Postback.
- No code is attached.
Now below happens:
- First time when page appears, controls will be containing there declarative values.
- Textbox : Textbox1’
- Label : ‘Lable1’
- Click on ‘Change Button’: When the page renders, Controls will be containing the programmatically assigned values
- Textbox : "Change 1";
- Label : "Change";
- Click on ‘Postback Button’: When the page renders, Controls will be containing the below values.
- Textbox : "Change 1";
- Label : " Lable1’"; [Will be default to its declarative value.]
Now enable the ViewState for the Label control. Then in the third step [Above Exercise], you will find that the changed value appears.
So the conclusion is that controls which inherit from the IPostBackDataHandler interface will retain their values even if the ViewState has been disabled.
Regards,
Arun…..
DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.
Arun,
ReplyDeletedropdown vs textbox ??
I created a simple web page with the dropdown list, and then changed it's items using javascript. (onclick event of another html button on the same page)
After that, when I did the postback using an asp button, my dropdown does not retain new values and I am not able to read that value using dropdown's selectedValue property in the code behind. (Although I was able to retrevie it using request.form)
Thats where I think textboxes are different from dropdowns, because if you change the value of textbox using javascript you can retrieve new value easily in code behind (using textbox1.value) and new value is also retained.
Thanks for the comments Jatin.
ReplyDeleteBut let me tell u the fact.
- Changes made at JS works regardless of the ViewState. i.e Control State is not dependent on Viewstate when changes are made at JS side.
- However, mostly the changes made at JS will be reset, once the postback occurs. i.e The controls will set to their decalrative values again on 'SimplePostBack' operation.
- But, still the state for DropDown, TextBox & Hidden Field, does not reset, even after the postback occurs.
So in an all, the changes made in JS in the DropDown, TextBox & Hidden Field will be retained even after postback occurs.
Soon, I'll pass you the code in ASP.net Forums link.
Regards,
Arun..