页面处理
事件 | 作业 |
Page.PreLinit | 通过IsPostBack属性确定是否第一次执行处理该页面、创建动态控件、动态设置主题属性、读取配置文件属性等 |
Page.Init | 初始化控件 |
Page.Load | 读取和更新控件属性 |
控件事件 | 如Button控件的Click事件 |
事件处理的顺序是Page.PreLinit、Page.Init、Page.Load和控件事件。
控件事件以Click和Changed事件为主。
Click事件会重新触发Page.Load
Changed事件需要立即执行,把AutoPostBack属性值设为true,这种设置太多会降低系统的运行效率。
IsPostBack属性
判断页面是否一次执行可以用本属性,默认值是false,第一次执行完后会属性值为false。
<!--WebForm3.aspx文件--> <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Project2_2.WebForm3" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /> </div> </form> </body> </html>
//WebForm3.aspx.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Project2_2 { public partial class WebForm3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Response.Write("页面第一次执行"); } } protected void Button1_Click(object sender, EventArgs e) { Response.Write("执行了Click事件"); } } }