Monday, June 27, 2011

Why does TextBox retains its value after multiple postbacks even if we set the EnableViewState property of the TextBox to False?

The reason being is ASP.NET uses this primitive to update the control's value. ASP.NET uses IPostBackDataHandler  for the controls that load the data from the form collection.
Actually all the controls which implement IPostbackdatahandler, implement the method LoadPostData and
RaisePostDataChangedEvent. But here the key method is LoadPostData, which returns true if the posted value is changed from earlier value and updates it with posted value, else it returns false.

As from the Page Life Cycle, we can see LoadPostData is called after the LoadViewState, whether viewstate is on or not,  it gets populated from the posted data. That's why the data get persisted even if viewstate is set to off for few controls. Following is the complete list of the controls, those implement IPostBackDataHandler.
  •  CheckBox
  •  CheckBoxList
  •  DropDownList
  •  HtmlInputCheckBox
  •  HtmlInputFile
  •  HtmlInputHidden
  •  HtmlInputImage
  •  HtmlInputRadioButton
  •  HtmlInputText
  •  HtmlSelect
  •  HtmlTextArea
  •  ImageButton
  •  ListBox
  •  RadioButtonList
  •  TextBox

No comments:

Post a Comment