ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - ʼþ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET - ʼþ


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-03 ä¯ÀÀ:3175 :: ::

An Event Handler is a subroutine that executes code for a given event.
ʼþ´¦ÀíÆ÷Ϊһ¸ö×Ó³ÌÐò£¬Ëü¿ÉÒÔΪָ¶¨µÄʼþÖ´ÐÐÏàÓ¦´úÂë¡£


ASP.NET - Event Handlers
ASP.NET - ʼþ´¦ÀíÆ÷

Look at the following code:
¿´¿´ÒÔÏ´úÂ룺

<%
lbl1.Text="ÈÕÆÚºÍʱ¼äΪ: " & 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
Page_Load ʼþ

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:
Page_Load ʼþÊÇËùÓÐASP.NETʼþÖеÄÒ»¸ö¡£Page_Load ʼþÔÚÒ³Ãæ¼ÓÔØÊ±¸øÓè°ó¶¨£¬È»ºóASP.NET»á×Ô¶¯µÄµ÷ÓÃPage_LoadµÄ×Ó³ÌÐò²¢Ö´ÐÐÔÚÀïÃæµÄ´úÂ룺

<script runat="server">
Sub Page_Load
lbl1.Text="ÈÕÆÚºÍʱ¼äΪ£º " & now()
End Sub
</script>
<html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html>

Note: The Page_Load event contains no object references or event arguments!
×¢Ò⣺Page_Load ʼþ²»°üº¬Èκβο¼¶ÔÏó»òÊÇʼþ²ÎÊý£¡

Example£¨¾ÙÀý£©


The Page.IsPostBack Property
Page.IsPostBack ÊôÐÔ

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):
Page_Load ×Ó³ÌÐò»áÔÚÒ³ÃæÃ¿´Î¼ÓÔØµÄʱºòÖ´ÐС£Èç¹ûÄãÏëÒªÔÚPage_Load ×Ó³ÌÐòÀïµÄ´úÂëÖ»ÔÚÒ³ÃæµÚÒ»´Î¼ÓÔØµÄʱºòÖ´Ðеϰ£¬Äã¿ÉÒÔʹÓÃPage.IsPostBackÊôÐÔ¡£Èç¹ûPage.IsPostBackÊôÐÔΪfalse£¬Ò³»áÔÚµÚÒ»´Îʱ½øÐмÓÔØ£¬Èç¹ûΪtrue£¬Ò³Ãæ»á»ØË͸ø·þÎñÆ÷£º

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
lbl1.Text="ÈÕÆÚºÍʱ¼äΪ£º " & 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>

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.
¾ÙÀýÖÐËù¿´µ½µÄ“The date and time is...”ÐÅÏ¢Ö»ÓÐÔÙÒ³ÃæµÚÒ»´Î½øÐмÓÔØÊ±²Å»áÊä³ö¡£µ±Óû§µã»÷Ìá½»°´Å¥£¬Ìá½»µÄ×Ó³ÌÐò»áÔÙµÚ¶þ¸ölabelÖÐÊä³ö“Hello World!”£¬µ«ÔÚµÚÒ»¸ölabelÖеÄÈÕÆÚºÍʱ¼ä²»»á¸Ä±ä¡£

Example£¨¾ÙÀý£©

ÆÀÂÛ (0) All