ASP.Net/Page/ViewState

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Page Enable View State

<%@ Page Language="vb" EnableViewState="True" %>
<html>
   <head>
      <title></title>
      <script runat="server">
         Sub Page_Load()
            If Page.EnableViewState = True Then
               Message.Text = "ViewState is enabled."
            Else
               Message.Text = "ViewState is disabled."
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>



ViewState property example

<%@ Page Language="vb" %>
<html>
   <head>
      <title>ViewState property example</title>
      <script runat="server">
         Sub Page_Load()
            Dim LocalColor As System.Drawing.Color
            If IsPostBack Then
               LocalColor = CType(ViewState("LabelColor"), System.Drawing.Color)
               If LocalColor.Name = "Black" Then
                  LocalColor = System.Drawing.Color.Red
               Else
                  LocalColor = System.Drawing.Color.Black
               End If
               Message.ForeColor = LocalColor
               Message.Text = "Label color is " & LocalColor.Name
               ViewState("LabelColor") = LocalColor
            Else
               Message.ForeColor = System.Drawing.Color.Black
               LocalColor = Message.ForeColor
               Message.Text = "Label color is " & LocalColor.Name
               ViewState("LabelColor") = LocalColor
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:button id="button" 
         text="Click to change label color"
         runat="server"/>
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>