📄 aspnet_events.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET Event Handlers</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP.NET - Events</h1>
<a href="aspnet_controls.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_forms.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">An Event Handler is a subroutine that executes code for a given
event.</p>
<hr />
<h2>ASP.NET - Event Handlers</h2>
<p>Look at the following code:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%
lbl1.Text="The date and time is " & now()
%></pre>
<pre><html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html></pre>
</td></tr>
</table>
<p>When will the code above be executed? The answer is: "You don't know..."</p>
<hr />
<h2>The Page_Load Event</h2>
<p>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:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
Sub Page_Load
lbl1.Text="The date and time is " & now()
End Sub
</script></pre>
<pre><html>
<body>
<form runat="server">
<h3><asp:label id="lbl1" runat="server" /></h3>
</form>
</body>
</html></pre>
</td></tr>
</table>
<p>
<b>Note:</b> The Page_Load event contains no object references or event
arguments!</p>
<p><a target="_blank" href="showasp.asp@filename=demo_pageload">Example</a></p>
<hr />
<h2>The Page.IsPostBack Property</h2>
<p>
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):</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
lbl1.Text="The date and time is " & now()
end if
End Sub</pre>
<pre>Sub Submit(s As Object, e As EventArgs)
lbl2.Text="Hello World!"
End Sub
</script></pre>
<pre><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></pre>
</td></tr>
</table>
<p>
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. </p>
<p><a target="_blank" href="showasp.asp@filename=demo_pageispostback">Example</a></p>
<hr />
<a href="aspnet_controls.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_forms.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -