ASP.NET - Events
An Event Handler//事件处理器 is a subroutine//子程序 that executes code for a given//给定的 event.
ASP.NET - Event Handlers
Look at the following code:
<%
lbl1.Text="The date and time is " & now()
%>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>
|
When will the code above be executed? The answer is: "You don't know..."
The Page_Load Event
The Page_Load event is one of many events//众多事件中的一个 that ASP.NET understands. The Page_Load event//载入页面事件 is triggered//触发 when a page loads, and ASP.NET will automatically call the subroutine Page_Load//载入页面子程序, and execute the code inside it:
<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>//属于 web server controls
</form>
</body>
</html>
|
Note: The Page_Load event//载入页面事件 contains no object references//参考 or event arguments//参数!
Example:
ASPX Source:
| <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> |
Output Result:
The date and time is 1/15/2007 8:09:28 PM |
The Page.IsPostBack//页面是否后退 Property//属性
The Page_Load subroutine runs EVERY time the page is loaded. If you want to execute the code in the Page_Load subroutine only the FIRST time the page is loaded, you can use the Page.IsPostBack property. If the Page.IsPostBack property is false, the page is loaded for the first time, if it is true, the page is posted back to the server (i.e. from a button click on a form):
<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>//属于web server controls
<h3><asp:label id="lbl2" runat="server" /></h3>//属于web server controls
<asp:button text="Submit" onclick="submit" runat="server" />//属于validation server controls
</form>
</body>
</html>
|
The example above will write the "The date and time is...." message only the first time the page is loaded. When a user clicks on the Submit button, the submit subroutine//子程序 will write "Hello World!" to the second label//标签, but the date and time in the first label will not change.
Example:
ASPX Source:
| <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> |
Output Result:
a.
The date and time is 1/15/2007 9:08:58 PM
|
b.Output Result:
after clicking the button "submit"
The date and time is 1/15/2007 9:37:11 PMHello World! |
浙公网安备 33010602011771号