Control.SaveViewState Method
Saves any server control view-state changes that have occurred since the time the page was posted back to the server.
[Visual Basic] Protected Overridable Function SaveViewState() As Object [C#] protected virtual object SaveViewState(); [C++] protected: virtual Object* SaveViewState(); [JScript] protected function SaveViewState() : Object;
Return Value
Returns the server control's current view state. If there is no view state associated with the control, this method returns a null reference (Nothing in Visual Basic).
Remarks
View state is the accumulation of the values of a server control's properties. These values are automatically placed in the server control's ViewState property, which is an instance of the StateBag class. This property's value is then persisted to a string object after the save state stage of the server control life cycle. For more information, see Control Execution Lifecycle.
When view state is saved, this string object is returned to the client as a variable that is stored in an HTML Hidden element. When you author custom server controls, you can improve efficiency by overriding this method and modifying your server control's ViewState property. For more information, see Methods in ASP.NET Server Controls and Maintaining State in a Control.
Example
[Visual Basic, C#, C++] The following example overrides the SaveViewState method in a custom ASP.NET server control. When this method is invoked, it determines whether the control has any child controls and whether the containing Page object is the result of a postback. If both are true, it changes the Text property of a Label Web server control to read Custom Control Has Saved State. It then saves the view state of the control as an array of objects, named allStates.
[Visual Basic] Protected Overrides Function SaveViewState() As Object ' Change Text Property of Label when this function is invoked. If HasControls() And Page.IsPostBack Then CType(Controls(0), Label).Text = "Custom Control Has Saved State" End If ' Save State as a cumulative array of objects. Dim baseState As Object = MyBase.SaveViewState() Dim _userText As String = UserText Dim _passwordText As String = PasswordText Dim allStates(3) As Object allStates(0) = baseState allStates(1) = _userText allStates(2) = PasswordText Return allStates End Function [C#] protected override object SaveViewState() { // Change Text Property of Label when this function is invoked. if(HasControls() && (Page.IsPostBack)) { ((Label)(Controls[0])).Text = "Custom Control Has Saved State"; } // Save State as a cumulative array of objects. object baseState = base.SaveViewState(); string userText = UserText; string passwordText = PasswordText; object[] allStates = new object[3]; allStates[0] = baseState; allStates[1] = userText; allStates[2] = PasswordText; return allStates; } [C++] protected: Object* SaveViewState() { // Change Text Property of Label when this function is invoked. if(HasControls() && (Page->IsPostBack)) { (dynamic_cast<Label*>(Controls->Item[0]))->Text = S"Custom Control Has Saved State"; } // Save State as a cumulative array of objects. Object* baseState = Control::SaveViewState(); String* userText = UserText; String* passwordText = PasswordText; Object* allStates[] = new Object*[3]; allStates[0] = baseState; allStates[1] = userText; allStates[2] = PasswordText; return allStates; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
Control Class | Control Members | System.Web.UI Namespace | LoadViewState | ViewState | StateBag | Methods in ASP.NET Server Controls | Maintaining State in a Control