This documentation is archived and is not being maintained.

UserControl.SaveViewState Method

Saves any user control view-state changes that have occurred since the last page postback.

[Visual Basic]
Overrides Protected Function SaveViewState() As Object
[C#]
protected override object SaveViewState();
[C++]
protected: Object* SaveViewState();
[JScript]
protected override function SaveViewState() : Object;

Return Value

Returns the user control's current view state. If there is no view state associated with the control, it returns a null reference (Nothing in Visual Basic).

Remarks

In general, you do not need to call this method. However, if you store custom information in view state, you can override this method to do so.

Example

[Visual Basic, C#, C++] The following example demonstrates a user control that manages its view state using the LoadViewState and SaveViewState methods.

[Visual Basic] 

Public ReadOnly Property UserText() As String
   Get
      Return CStr(ViewState("usertext"))
   End Get
End Property

Public ReadOnly Property PasswordText() As String
   Get
      Return CStr(ViewState("passwordtext"))
   End Get
End Property

<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub LoadViewState(savedState As Object)
   If Not (savedState Is Nothing) Then
      MyBase.LoadViewState(savedState)
   End If
End Sub 
  
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Function SaveViewState() As Object
   ViewState("usertext") = user.Text
   ViewState("passwordtext") = password.Text
   Return MyBase.SaveViewState()
End Function 
End Class 

[C#] 
public string UserText
{
   get
   {
      return (string)ViewState["usertext"];
   }
}
public string PasswordText
{
   get
   {
      return (string)ViewState["passwordtext"];
   }
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
protected override void LoadViewState(object savedState) 
{
   if (savedState != null)
      base.LoadViewState(savedState);
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
protected override object SaveViewState()
{
   ViewState["usertext"] = user.Text;
   ViewState["passwordtext"] = password.Text;
   return base.SaveViewState();
}

[C++] 
public:
   __property String* get_UserText()
   {
      return dynamic_cast<String*>(ViewState->Item[S"usertext"]);
   }

   __property String* get_PasswordText()
   {
      return dynamic_cast<String*>(ViewState->Item[S"passwordtext"]);
   }

protected:
   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] 
   void LoadViewState(Object* savedState) 
   {
      if (savedState != 0)
         UserControl::LoadViewState(savedState);
   }

   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name=S"FullTrust")] 
   Object* SaveViewState()
   {
      ViewState->Item[S"usertext"] = user->Text;
      ViewState->Item[S"passwordtext"] = password->Text;
      return UserControl::SaveViewState();
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

UserControl Class | UserControl Members | System.Web.UI Namespace | Control.ViewState | StateBag

Show: