• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題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
    當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

    驗(yàn)證一個ASP.NET應(yīng)用程序和頁面的生命周期的實(shí)現(xiàn)代碼

    來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:42:26
    文檔

    驗(yàn)證一個ASP.NET應(yīng)用程序和頁面的生命周期的實(shí)現(xiàn)代碼

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

    如果我們能更好地掌握這樣一個過程,那么對單個ASP.NET Page的生命周期也能更好地了解:
    下面介紹如何編寫一個簡單的ASP.NET 頁面和一個簡單的HttpModule,對MSDN里提到的ASP.NET的生命周期進(jìn)行驗(yàn)證
    1. 首先使用Visual Studio 2010建立一個空的ASP.NET網(wǎng)站 (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文件夾,新建一個類,內(nèi)容為:
    代碼如下:

    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內(nèi)容如下:
    代碼如下:

    <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執(zhí)行,在瀏覽器里可以看到:

    7. 在文本框內(nèi)輸入內(nèi)容,可得:

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

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

    文檔

    驗(yàn)證一個ASP.NET應(yīng)用程序和頁面的生命周期的實(shí)現(xiàn)代碼

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

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 亚洲国产精品无码专区在线观看| 亚洲精品无码不卡在线播HE | 国产精品精品自在线拍| 精品久久久久久无码人妻热 | 精品91自产拍在线观看| 国产精品美女久久久| 人妻精品久久无码区| 欧美在线精品一区二区三区| 91在线手机精品超级观看| 91精品国产福利在线导航| 人妻少妇乱子伦精品| 亚洲午夜精品久久久久久浪潮| 精品国产亚洲男女在线线电影| 日韩精品在线观看视频| 国产精品免费高清在线观看| 亚洲国产精品无码久久一线 | 精品不卡一区二区| 亚洲国产另类久久久精品黑人 | 国产精品 视频一区 二区三区| 99久久国语露脸精品国产| 久久久久久夜精品精品免费啦| 国产美女精品一区二区三区| 日韩精品欧美国产在线| 99re8这里有精品热视频免费| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久综合九色综合精品| 精品国产sm捆绑最大网免费站| 日本aⅴ精品中文字幕| 欧洲精品久久久av无码电影| 中文字幕无码精品三级在线电影 | 亚洲欧美日韩久久精品| 一本之道av不卡精品| 亚洲?V无码乱码国产精品 | 午夜精品久久久久9999高清| 亚洲精品第一国产综合精品99| 欧美精品一区二区三区免费| 国产精品国产AV片国产| 无码精品视频一区二区三区| 亚洲国产精品久久久久网站| 日韩精品在线看| 国产99久久九九精品无码|