ASP. NET Default. aspx File

Source: Internet
Author: User

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

 
 
  1. Publicclass_Default: System. Web. UI. Page
  2. {
  3. PrivatevoidPage_Load (objectsender, System. EventArgse)
  4. {
  5. // Place user code here to initialize the page
  6. }
  7. }
  8. }

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.

 
 
  1. <% @Pagelanguage= "C #"Codebehind= "Default. aspx. cs"AutoEventWireup=
    "False"Inherits= "MyTest. _ Default" %> 
  2. <! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN"> 
  3. <HTML> 
  4. <HEAD> 
  5. <Title>Default</Title> 
  6. <MetanameMetaname= "GENERATOR"Content= "MicrosoftVisualStudio. NET7.1"> 
  7. <MetanameMetaname= "CODE_LANGUAGE"Content= "C #"> 
  8. <MetanameMetaname= "Vs_defaultClientScript"Content= "JavaScript"> 
  9. <MetanameMetaname= "Vs_targetSchema"Content=
    Http://schemas.microsoft.com/intellisense/ie5"> 
  10. </HEAD> 
  11. <BodyMS_POSITIONINGBodyMS_POSITIONING="GridLayout"> 
  12. <FormidFormid= "Form1"Method= "Post"Runat="Server"> 
  13. <FONTfaceFONTface=""></FONT> 
  14. </Form> 
  15. </Body> 
  16. </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.

 
 
  1. Publicclass_Default: System. Web. UI. Page
  2. {
  3. PrivatevoidPage_Load (objectsender, System. EventArgse)
  4. {
  5. // Place user code here to initialize the page
  6. }
  7.  
  8. Code generated by Web form designer # code generated by regionWeb Form Designer
  9. OverrideprotectedvoidOnInit (EventArgse)
  10. {
  11. //
  12. // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
  13. //
  14. InitializeComponent ();
  15. Base. OnInit (e );
  16. }
  17.  
  18. /**////<Summary> 
  19. /// The designer supports the required methods-do not use the code editor to modify
  20. /// Content of this method.
  21. ///</Summary> 
  22. PrivatevoidInitializeComponent ()
  23. {
  24. This. Load + = newSystem. EventHandler (this. Page_Load );
  25.  
  26. }
  27. # Endregion
  28. }

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:

 
 
  1. UsingSystem;
  2. UsingSystem. Web;
  3.  
  4. NamespaceMyTest
  5. {
  6. /**////<Summary> 
  7. /// Summary of MyTest.
  8. ///</Summary> 
  9. PublicclassMPage: System. Web. UI. Page
  10. {
  11. PublicMPage ()
  12. {
  13. //
  14. // TODO: add the constructor logic here
  15. //
  16. Try
  17. {
  18. If (HttpContext. Current. Application. Count<1)
  19. HttpContext. Current. Application. Add ("Title", "my website ");
  20. // Run the command when the Application is not created
  21. }
  22. Catch
  23. {
  24. HttpContext. Current. Response. Redirect ("Err. Htm", true );
  25. // When an error occurs, jump to the error page
  26. }
  27. }
  28.  
  29. PublicstringMy_Title
  30. {
  31. Get
  32. {
  33. ReturnApplication ["Title"]. ToString ();
  34. }
  35. }
  36. }
  37. }

Default. aspx. cs inheritance

 
 
  1. UsingSystem;
  2. UsingSystem. Collections;
  3. UsingSystem. ComponentModel;
  4. UsingSystem. Data;
  5. UsingSystem. Drawing;
  6. UsingSystem. Web;
  7. UsingSystem. Web. SessionState;
  8. UsingSystem. Web. UI;
  9. UsingSystem. Web. UI. WebControls;
  10. UsingSystem. Web. UI. HtmlControls;
  11.  
  12. NamespaceMyTest
  13. {
  14. /**////<Summary> 
  15. /// _ Default abstract description.
  16. ///</Summary> 
  17. Publicclass_Default: MPage
  18. {
  19. PrivatevoidPage_Load (objectsender, System. EventArgse)
  20. {
  21. // Place user code here to initialize the page
  22. }
  23.  
  24. Code generated by Web form designer # code generated by regionWeb Form Designer
  25. OverrideprotectedvoidOnInit (EventArgse)
  26. {
  27. //
  28. // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
  29. //
  30. InitializeComponent ();
  31. Base. OnInit (e );
  32. }
  33.  
  34. /**////<Summary> 
  35. /// The designer supports the required methods-do not use the code editor to modify
  36. /// Content of this method.
  37. ///</Summary> 
  38. PrivatevoidInitializeComponent ()
  39. {
  40. This. Load + = newSystem. EventHandler (this. Page_Load );
  41.  
  42. }
  43. # Endregion
  44. }
  45. }

ASP. NET Default. aspx File Usage

 
 
  1. <% @Pagelanguage= "C #"Codebehind= "Default. aspx. cs"AutoEventWireup=
    "False"Inherits= "MyTest. _ Default" %> 
  2. <! DOCTYPEHTMLPUBLIC "-// W3C // DTDHTML4.0Transitional // EN"> 
  3. <HTML> 
  4. <HEAD> 
  5. <Title><% = My_Title %></Title> 
  6. <MetanameMetaname= "GENERATOR"Content= "MicrosoftVisualStudio. NET7.1"> 
  7. <MetanameMetaname= "CODE_LANGUAGE"Content= "C #"> 
  8. <MetanameMetaname= "Vs_defaultClientScript"Content= "JavaScript"> 
  9. <MetanameMetaname= "Vs_targetSchema"Content=
    Http://schemas.microsoft.com/intellisense/ie5"> 
  10. </HEAD> 
  11. <BodyMS_POSITIONINGBodyMS_POSITIONING="GridLayout"> 
  12. <FormidFormid= "Form1"Method= "Post"Runat="Server"> 
  13. <FONTfaceFONTface=""></FONT> 
  14. </Form> 
  15. </Body> 
  16. </HTML> 
  1. Analysis of Theme functions in ASP. NET development skills
  2. ASP. NET Dynamic Compilation
  3. Analysis on ASP. NET supported by Apache
  4. Introduction to ASP. NET Server standard controls
  5. Analysis on SQL Server Database Backup Recovery in ASP. NET

Related Article

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.