First, we can see that in both 1.1 and 2.0, each aspx file of the website created with VS corresponds to a cs file (or other language such as vb ). the class in the cs file is generally the same as the file name. If you encounter a keyword, it will automatically add '_' before the class name, for example: _ Default. the created cs file inherits from System. web. UI. page class, and only \ must inherit this class. the reason is that the aspx file inherits the cs file.
Take the Default. aspx, Default. aspx. cs file as an example. Default. aspx. cs contains the class _ Default, which inherits the word "System. Web. UI. Page ".
Pass
- Publicclass_Default: System. Web. UI. Page
- {
- PrivatevoidPage_Load (objectsender, System. EventArgse)
- {
- // Place user code here to initialize the page
- }
- }
- }
You can see it.
While the ASP. NET Default. aspx file does not clarify which class to use, it actually inherits from the _ Default class.
- <% @Pagelanguage= "C #"Codebehind= "Default. aspx. cs"AutoEventWireup=
"False"Inherits= "MyTest. _ Default" %>
- <! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN">
- <HTML>
- <HEAD>
- <Title>Default</Title>
- <MetanameMetaname= "GENERATOR"Content= "MicrosoftVisualStudio. NET7.1">
- <MetanameMetaname= "CODE_LANGUAGE"Content= "C #">
- <MetanameMetaname= "Vs_defaultClientScript"Content= "JavaScript">
- <MetanameMetaname= "Vs_targetSchema"Content=
Http://schemas.microsoft.com/intellisense/ie5">
- </HEAD>
- <BodyMS_POSITIONINGBodyMS_POSITIONING="GridLayout">
- <FormidFormid= "Form1"Method= "Post"Runat="Server">
- <FONTfaceFONTface=""></FONT>
- </Form>
- </Body>
- </HTML>
Inherits = "MyTest. _ Default "indicates this. this file is created in VS2003, that is, ASP. NET1.1, MyTest is my namespace.
Class Running Mechanism
After talking about this, we didn't actually talk about the System. Web. HttpContext class. However, what we are talking about now is very helpful for the use of the System. Web. HttpContext class.
If we access the Default. aspx file, do we only access the ASP. NET Default. aspx file or the class? Of course not. When a subclass is requested in ASP. NET, the parent class is instantiated first. First, create the fields of the parent class, and then the constructor.
- Publicclass_Default: System. Web. UI. Page
- {
- PrivatevoidPage_Load (objectsender, System. EventArgse)
- {
- // Place user code here to initialize the page
- }
-
- Code generated by Web form designer # code generated by regionWeb Form Designer
- OverrideprotectedvoidOnInit (EventArgse)
- {
- //
- // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
- //
- InitializeComponent ();
- Base. OnInit (e );
- }
-
- /**////<Summary>
- /// The designer supports the required methods-do not use the code editor to modify
- /// Content of this method.
- ///</Summary>
- PrivatevoidInitializeComponent ()
- {
- This. Load + = newSystem. EventHandler (this. Page_Load );
-
- }
- # Endregion
- }
From the above class, we can see that this class has no fields or constructor. To instantiate it, you must first instantiate the System. Web. UI. Page class. The System. Web. UI. Page class is not discussed in this article. The OnInit method is first executed when the _ Default class is instantiated. It overwrites System. Web. UI. Page. OnInit. Then the InitializeComponent method is called, while the InitializeComponent method calls the Load event and executes the Page_Load method.
Before the Page_Load method is executed, many objects in the System. Web. UI. Page class cannot be used, such as Application and Session.
Use of the Current attribute of the System. Web. HttpContext class
I finally mentioned the focus of this Article. Maybe I won't talk much about this part, but it is actually here to serve. I don't want to talk about it anymore. I don't know how to read articles that are too long. What's more, I write a superficial article and it's not brilliant enough.
Application can be used as follows:
- UsingSystem;
- UsingSystem. Web;
-
- NamespaceMyTest
- {
- /**////<Summary>
- /// Summary of MyTest.
- ///</Summary>
- PublicclassMPage: System. Web. UI. Page
- {
- PublicMPage ()
- {
- //
- // TODO: add the constructor logic here
- //
- Try
- {
- If (HttpContext. Current. Application. Count<1)
- HttpContext. Current. Application. Add ("Title", "my website ");
- // Run the command when the Application is not created
- }
- Catch
- {
- HttpContext. Current. Response. Redirect ("Err. Htm", true );
- // When an error occurs, jump to the error page
- }
- }
-
- PublicstringMy_Title
- {
- Get
- {
- ReturnApplication ["Title"]. ToString ();
- }
- }
- }
- }
Default. aspx. cs inheritance
- UsingSystem;
- UsingSystem. Collections;
- UsingSystem. ComponentModel;
- UsingSystem. Data;
- UsingSystem. Drawing;
- UsingSystem. Web;
- UsingSystem. Web. SessionState;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- UsingSystem. Web. UI. HtmlControls;
-
- NamespaceMyTest
- {
- /**////<Summary>
- /// _ Default abstract description.
- ///</Summary>
- Publicclass_Default: MPage
- {
- PrivatevoidPage_Load (objectsender, System. EventArgse)
- {
- // Place user code here to initialize the page
- }
-
- Code generated by Web form designer # code generated by regionWeb Form Designer
- OverrideprotectedvoidOnInit (EventArgse)
- {
- //
- // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
- //
- InitializeComponent ();
- Base. OnInit (e );
- }
-
- /**////<Summary>
- /// The designer supports the required methods-do not use the code editor to modify
- /// Content of this method.
- ///</Summary>
- PrivatevoidInitializeComponent ()
- {
- This. Load + = newSystem. EventHandler (this. Page_Load );
-
- }
- # Endregion
- }
- }
ASP. NET Default. aspx File Usage
- <% @Pagelanguage= "C #"Codebehind= "Default. aspx. cs"AutoEventWireup=
"False"Inherits= "MyTest. _ Default" %>
- <! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN">
- <HTML>
- <HEAD>
- <Title><% = My_Title %></Title>
- <MetanameMetaname= "GENERATOR"Content= "MicrosoftVisualStudio. NET7.1">
- <MetanameMetaname= "CODE_LANGUAGE"Content= "C #">
- <MetanameMetaname= "Vs_defaultClientScript"Content= "JavaScript">
- <MetanameMetaname= "Vs_targetSchema"Content=
Http://schemas.microsoft.com/intellisense/ie5">
- </HEAD>
- <BodyMS_POSITIONINGBodyMS_POSITIONING="GridLayout">
- <FormidFormid= "Form1"Method= "Post"Runat="Server">
- <FONTfaceFONTface=""></FONT>
- </Form>
- </Body>
- </HTML>
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache
- Introduction to ASP. NET Server standard controls
- Analysis on SQL Server Database Backup Recovery in ASP. NET