ASP.NETイベントハンドラ
これは、サブルーチン内のコードを実行するために指定されたイベントのイベントハンドラです。
ASP.NET - イベントハンドラ
次のコードを考えてみます。
<%
lbl1.Text="The date and time is " & now()
%>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
lbl1.Text="The date and time is " & now()
%>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
上記のコードは、ときに実行されるのですか? 答えは: "私は知りません...."
Page_Loadイベント
Page_Loadイベントは、ASP.NET理解できる多くのイベントの一つです。 Page_Loadイベントは、ページがロードされたとき、ASP.NETが自動的にサブルーチンをPage_Loadを呼び出しますトリガされ、コードを実行されます。
例
<script runat="server">
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
デモ>>
注:Page_Loadイベントにはオブジェクト参照またはイベント引数が含まれていません!
Page.IsPostBackプロパティ
ページが毎回ロードされるときをPage_Loadサブルーチンが実行されます。 あなたはときに、ページが最初にロードをPage_Loadサブルーチンのコードを実行したい場合は、Page.IsPostBackプロパティを使用することができます。 。trueに設定した場合Page.IsPostBackプロパティをfalseに設定すると、ページが最初にロードされ、そのページには、(例えば、フォーム上のボタンをクリックして)サーバに返送されます。
例
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
lbl1.Text="The date and time is " & now()
end if
End Sub
Sub submit(s As Object, e As EventArgs)
lbl2.Text="Hello World!"
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>
Sub Page_Load
if Not Page.IsPostBack then
lbl1.Text="The date and time is " & now()
end if
End Sub
Sub submit(s As Object, e As EventArgs)
lbl2.Text="Hello World!"
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
<h3><asp:label id="lbl2" runat="server" /></h3>
<asp:button text="Submit" onclick="submit" runat="server" />
</form>
</body>
</html>
デモ>>
上記の例は、「....日時がある」ページが最初にロードされたメッセージのみが表示されます。 ユーザーが[送信]ボタンをクリックすると、提出するサブルーチンは、第二の標識では "!のHello World"を書き込みますされていますが、最初のラベルの日付と時刻が変更されません。