Verify the implementation code for a asp.net application and the lifecycle of the page _ Practical tips

Source: Internet
Author: User
If we can better master such a process, we can better understand the lifecycle of a single asp.net page:
Here's how to write a simple asp.net page and a simple httpmodule to validate the lifecycle of the ASP.net mentioned in MSDN
1. First use Visual Studio 2010 to create an empty asp.net Web site (asp.net 4.0)
2. Add a default.aspx, add three ASP.net controls, Textbox,button and validator respectively:
Copy Code code as follows:
  
<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. Add a asp.net App_Code folder and create a new class that reads:
Copy Code code as follows:

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 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. Modify just the Default.aspx background CS code:
Copy Code code as follows:

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. The contents of the modified Web.config are as follows:
Copy Code code as follows:

<configuration>
<system.web>
<compilation debug= "True" targetframework= "4.0"/>
</system.web>
<system.web>
<add name= "TestClass" type= "TestClass"/>
</system.web>
</configuration>

6. Ctrl+f5 execution, you can see in the browser:

7. Enter content in the text box, you can get:


Conclusion:
1. Module is initialized only once, and the module is not initialized when the page is postback. The
2. Validate and event events do not fire when the page is first initialized, but because the page itself has validate controls and events buttons, the two events are triggered the second time.
This article refers to the following article in CodeProject.com http://www.codeproject.com/KB/aspnet/ASPDOTNETPageLifecycle.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.