<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼

        來源:懂視網 責編:小采 時間:2020-11-27 22:42:26
        文檔

        驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼

        驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼:如果我們能更好地掌握這樣一個過程,那么對單個ASP.NET Page的生命周期也能更好地了解: 下面介紹如何編寫一個簡單的ASP.NET 頁面和一個簡單的HttpModule,對MSDN里提到的ASP.NET的生命周期進行驗證 1. 首先使用Visual Studio 2010建立一個
        推薦度:
        導讀驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼:如果我們能更好地掌握這樣一個過程,那么對單個ASP.NET Page的生命周期也能更好地了解: 下面介紹如何編寫一個簡單的ASP.NET 頁面和一個簡單的HttpModule,對MSDN里提到的ASP.NET的生命周期進行驗證 1. 首先使用Visual Studio 2010建立一個

        如果我們能更好地掌握這樣一個過程,那么對單個ASP.NET Page的生命周期也能更好地了解:
        下面介紹如何編寫一個簡單的ASP.NET 頁面和一個簡單的HttpModule,對MSDN里提到的ASP.NET的生命周期進行驗證
        1. 首先使用Visual Studio 2010建立一個空的ASP.NET網站 (ASP.NET 4.0)
        2. 添加一個Default.aspx,添加三個ASP.NET控件,分別為TextBox,Button和Validator:
        代碼如下:  
        <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="OK" onclick="btnSubmit_Click" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please input your name!" ControlToValidate="txtName" ForeColor="#FF3300">
        </asp:RequiredFieldValidator>
        </div>
        </form>

        3. 添加一個ASP.NEt的App_code文件夾,新建一個類,內容為:
        代碼如下:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        public class TestClass : IHttpModule
        {
        HttpApplication httpApp;
        public static List<string> EventList = new List<string>();
        public TestClass()
        {
        }
        public void Dispose()
        { }
        public void Init(HttpApplication context)
        {
        this.httpApp = context;
        //EventList.Clear();
        EventList.Add("Initiated");
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);
        context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
        context.ResolveRequestCache += new EventHandler(context_ResolveRequestCache);
        context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState);
        context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
        context.UpdateRequestCache += new EventHandler(context_UpdateRequestCache);
        context.EndRequest += new EventHandler(context_EndRequest);
        }
        private void context_EndRequest(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: End Request <hr>");
        foreach (string str in EventList)
        {
        httpApp.Response.Write(str + "<br>");
        }
        EventList.Clear();
        }
        void context_UpdateRequestCache(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Update Request Cache");
        }
        void context_ReleaseRequestState(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Release Request State");
        }
        void context_PostReleaseRequestState(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Post Release Request State");
        }
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Pre Request Handler Execution");
        }
        void context_AcquireRequestState(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Acquire Request State");
        }
        void context_ResolveRequestCache(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Resolve Request");
        }
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Authorize Request");
        }
        void context_AuthenticateRequest(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: AuthenticateRequest");
        }
        void context_BeginRequest(object sender, EventArgs e)
        {
        EventList.Add("HTTP Modules: Begin Request");
        }
        }

        4. 修改剛才的Default.aspx的后臺cs代碼:
        代碼如下:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        public partial class _Default : System.Web.UI.Page
        {
        protected void Page_Init()
        {
        TestClass.EventList.Add("ASP.NET Page: Page_Init");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        TestClass.EventList.Add("ASP.NET Page: Page_Load");
        }
        public override void Validate()
        {
        TestClass.EventList.Add("ASP.NET Page: Validated");
        base.Validate();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
        TestClass.EventList.Add("ASP.NET Page: Event");
        }
        protected override void Render(HtmlTextWriter writer)
        {
        TestClass.EventList.Add("ASP.NET Page: Render");
        base.Render(writer);
        }
        protected void Page_Unload(object sender, EventArgs e)
        {
        TestClass.EventList.Add("ASP.NET Page: Unload");
        }
        }

        5. 修改web.config內容如下:
        代碼如下:

        <configuration>
        <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        </system.web>
        <system.web>
        <httpModules>
        <add name="TestClass" type="TestClass"/>
        </httpModules>
        </system.web>
        </configuration>

        6. Ctrl+F5執行,在瀏覽器里可以看到:

        7. 在文本框內輸入內容,可得:

         
        結論:
        1. Module只初始化了一次,當頁面postback的時候,module不會再初始化。
        2. Validate和Event事件在頁面第一次初始化的時候不會觸發,但是由于頁面本身存在validate控件和事件按鈕,所以這兩個事件在第二次會被觸發。
        本文參考了codeproject.com的如下一篇文章http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼

        驗證一個ASP.NET應用程序和頁面的生命周期的實現代碼:如果我們能更好地掌握這樣一個過程,那么對單個ASP.NET Page的生命周期也能更好地了解: 下面介紹如何編寫一個簡單的ASP.NET 頁面和一個簡單的HttpModule,對MSDN里提到的ASP.NET的生命周期進行驗證 1. 首先使用Visual Studio 2010建立一個
        推薦度:
        標簽: 頁面 代碼 周期
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费国产成人午夜私人影视| 久久久www成人免费毛片| 亚洲国产成人乱码精品女人久久久不卡| 激情亚洲一区国产精品| 国产a视频精品免费观看| 亚洲国产综合精品| 毛片免费vip会员在线看| 亚洲综合精品伊人久久| 国产免费观看黄AV片| 美女被暴羞羞免费视频| 中文字幕亚洲图片| 亚洲视频免费在线观看| 亚洲一级免费毛片| 国产成人免费网站在线观看| 日韩免费高清一级毛片| 亚洲精品无码乱码成人| 真实国产乱子伦精品免费| 最新亚洲精品国偷自产在线| 免费v片在线观看| a级毛片免费高清毛片视频| 亚洲自偷自拍另类12p| 免费毛片在线看片免费丝瓜视频 | a高清免费毛片久久| 亚洲女同成av人片在线观看| 99精品视频在线免费观看| 伊人久久亚洲综合影院首页| 国产免费观看网站| 一级毛片免费播放| 亚洲av纯肉无码精品动漫| 亚洲欧洲日产国码无码久久99| 亚洲一区二区三区免费在线观看| 亚洲国产成人无码AV在线| 中文字幕人成人乱码亚洲电影| 999国内精品永久免费视频| 无遮挡a级毛片免费看| 亚洲第一福利视频| 国产精品色午夜免费视频| 国产精品免费看久久久| 亚洲Av永久无码精品黑人| 亚洲成AV人片在线观看无码 | 亚洲精品线路一在线观看|