ASP.NET Web Forms - 事件

约 1042 字 更新于 2026-11-18 5iyo.com

ASP.NET Web Forms - 事件


事件句柄是一种针对给定事件来执行代码的子例程。


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>

上面的代码将在何时被执行?答案是:"不知道..."。


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>

演示实例 »

注释:Page_Load 事件不包含对象引用或事件参数!


Page.IsPostBack 属性

实例

<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" runat="server" />
</form>
</body>
</html>

演示实例 »