VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网站开发 > ASP.net 4.0教程 >
  • asp.net教程之Web Forms - 事件

事件句柄是一种针对给定事件来执行代码的子例程。

ASP.NET - 事件句柄

请看下面的代码:


  1. <% lbl1.Text="The date and time is " & now() %> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> </form> </body> </html>
复制

上面的代码将在何时被执行?答案是:"不知道..."。

Page_Load 事件

Page_Load 事件是 ASP.NET 可理解的众多事件之一。Page_Load 事件会在页面加载时被触发, ASP.NET 将自动调用 Page_Load 子例程,并执行其中的代码:

实例


  1. <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>
复制
注释:Page_Load 事件不包含对象引用或事件参数!

Page.IsPostBack 属性

Page_Load 子例程会在页面每次加载时运行。如果您只想在页面第一次加载时执行 Page_Load 子例程中的代码,那么您可以使用 Page.IsPostBack 属性。如果 Page.IsPostBack 属性设置为 false,则页面第一次被载入,如果设置为 true,则页面被传回到服务器(比如,通过点击表单上的按钮):

实例


  1. <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>
复制
上面的实例仅在页面第一次加载时显示 "The date and time is...." 消息。当用户点击 Submit 按钮是,submit 子例程将会在第二个 label 中写入 "Hello World!",但是第一个 label 中的日期和时间不会改变。
 

相关教程