ASP.Net/Page/ViewState — различия между версиями

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

Текущая версия на 11:53, 26 мая 2010

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>