Thursday, October 16, 2008
If you observe that even though we disable ViewState for TextBox control the state of the textbox is preserved i.e the text entered in textbox is preserved on page postbacks.
This uses Control State. It is a way to save controls state information when EnableViewState property is set to false for a control. We can use this whenever we feel that control wont work in desired manner without sate information. To maintain state information even though Enable ViewState is made false.
We can implement this for custom controls in the same way it is implimented for TextBox. To impliment Control State first we need to override the OnInit method of the control and add the call for the Page.RegisterRequiresControlState method with the instance of the control to register. Then, override the LoadControlState and SaveControlState in order to save the required state information. Sample code is as shown below.
public class ControlStateWebControl : Control
private string _strStateToSave;
protected override void OnInit(EventArgs e)
protected override object SaveControlState()
protected override void LoadControlState(object state)
if (state != null)
_strStateToSave = state.ToString();
Also Read other Top Articles
- JSON Serialization in VS 2008
- Implementing Forms Authentication in Silverlight Application.
- Making GridView Rows or Individual Cells Clickable and Selectable.
- Enabling browser back button for GridView Paging and Sorting in Ajax 1.1 and 3.5 (using Visual Studio 2005/ Visual studio 2008)
- How to pass values from User Control to Page or calling Page methods from User Control.
- What is WCF?
- New features in C# 4.0
- C# to VB.NET and VB.NET to C# online free converter tools.