当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - 事件

ASP.NET
ASP.NET 2.0 - Navigation 导航
C#中new和override区别
ASP.NET中 Bin,App_Browser,App_code,App_Data,App_Theme 等文件

ASP.NET - 事件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1174 ::
收藏到网摘: n/a

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(举例)

评论 (2) 1 All

登陆 | 还没注册?