w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - ʼþ
An Event Handler is a subroutine that executes code for a given event.
ʼþ´¦ÀíÆ÷Ϊһ¸ö×Ó³ÌÐò£¬Ëü¿ÉÒÔΪָ¶¨µÄʼþÖ´ÐÐÏàÓ¦´úÂë¡£
Look at the following code:
¿´¿´ÒÔÏ´úÂ룺
<% <html> |
When will the code above be executed? The answer is: "You don't know..."
ʲôʱºò»áÖ´ÐÐÒÔÉϵĴúÂëÄØ£¿´ð°¸ÊÇ“Äã²»ÖªµÀ...”
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"> <html> |
Note: The Page_Load event contains no object references or event arguments!
×¢Ò⣺Page_Load ʼþ²»°üº¬Èκβο¼¶ÔÏó»òÊÇʼþ²ÎÊý£¡
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 Submit(s As Object, e As EventArgs) <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ÖеÄÈÕÆÚºÍʱ¼ä²»»á¸Ä±ä¡£
ÆÀÂÛ (0)
All