ASP.NETを使ってみよう No3

ASP.NETについて調べた事のメモ!



smegheadさんのご意見を参考に、【ViewState】を使用した簡単なサンプルを作成してみました。

<%@ PAGE LANGUAGE="VB" Inherits="Test" Src="ViewStateCounter.aspx.vb" %>
<html>
<body>
  <form runat="server">
    <p>
      <asp:Label id="labCounter" runat="server" />
      <br>
      <asp:Button Text="up" runat="server" />
    </p>
  </form>
</body>
</html>

public Class Test
  Inherits System.Web.UI.Page

  protected labCounter As System.Web.UI.WebControls.Label

  Sub Page_Load(sender As object, e As System.EventArgs)
    Dim counter As Integer = 0
    If (IsPostBack) 
      If (Not(ViewState("counter") Is Nothing))
        counter = CType(ViewState("counter"),Integer)
        counter += 1
      End If
    End If
    labCounter.Text = counter.ToString()
    ViewState("counter") = counter
  End Sub
End Class