事件句柄是一种针对给定事件来执行代码的子例程。
请看下面的代码:
<% |
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 事件是 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> |
注释:
Page_Load 事件不包含对象引用或事件参数!
Page_Load 子例程会在页面每次加载时运行。如果您只想在页面第一次加载时执行 Page_Load 子例程中的代码,那么您可以使用 Page.IsPostBack 属性。如果 Page.IsPostBack 属性设置为 false,则页面第一次被载入,如果设置为 true,则页面被传回到服务器(比如,通过点击表单上的按钮):
<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> |