Basic Implementation of ASP. NET templates

Source: Internet
Author: User

Basic Implementation of ASP. NET templates

In the past or now), when we are working on a WEB project, we often use some shared files, such as the top header of the entire page), left navigation), bottom footer) wait for some common HTML, ASP, and other files. Even so, we also need to make the framework style for these files in advance. Is it generally table ?! A few of them will use DIV) to combine include) these files to get a complete page, a combination of these shared files, suppose that it is defined as a template, although the actual meaning of the template may be far more than this ). Even so, when the entire framework of our project changes, we should not modify the pages for which such templates are required for each HTML Tag defined by table and so on, although such work can be completed through ctrl + c and ctrl + v, the workload is also huge and time-consuming. The following is the basic implementation method of ASP. NET templates.

Development

In ASP. NET, it uses the object-oriented development mode, which can be understood in this way-every page is. UI. class inherited by Page. This Class provides services such as cache, representation, response, and request ). Is there a better solution through object-oriented methods than using include? Of course, the answer is yes.

Implementation

I remember a famous saying that "any problem can be achieved by adding an intermediate layer." For example, we often use the Fa C ade mode to reduce the Coupling Degree of the system, why should we use the design pattern? It is mainly used to reduce coupling and improve reuse.

All ASPX pages are composed of SystemWeb. UI. from this point of view, we only need. UI. adding a layer between pages and writing a Class can simplify the problem. in the. NET Framework, you can refer to Web User Controls to allow users to customize HTML code.

We join a middle layer. This custom class inherits the base class System. Web. UI. Page. The code for the custom class is as follows:

 
 
  1. PageBase. cs
  2. PublicclassPageBase: System. Web. UI. Page
  3. {
  4. PublicstringPageTitle="Test template";
  5. Protectedoverrisponidrender (System. Web. UI. HtmlTextWriterwriter)
  6. {
  7. Writer. Write (@"<Html><Head> 
  8. <Metahttp-equivMetahttp-equiv= 'Content-type'Content= 'Text/html;Charset=Gb2312'> 
  9. <Title>"+ This. PageTitle +"</Title></Head>");
  10. Writer. Write (@"<Body> 
  11. <TableborderTableborder= '0'Width= '000000'> 
  12. <Tr> 
  13. <TdwidthTdwidth= '000000'Bgcolor= '#006699'Align='Center'><FontcolorFontcolor='
    # Ffff'><B><AhrefAhref='Index. aspx'>Homepage</A></B></Font></Td> 
  14. <TdcolspanTdcolspan= '2'Width= '000000'>Advertisement bar</Td> 
  15. </Tr> 
  16. <Tr> 
  17. <TdwidthTdwidth= '000000'Valign= 'Top'> 
  18. <P>Navigation</P> 
  19. <P><AhrefAhref='Newcontact. aspx'>Add contact</A></P> 
  20. <P>Search for contacts</P> 
  21. </Tr> 
  22. <TdwidthTdwidth='10'> </Td> 
  23. <TdwidthTdwidth='123'> 
  24. ");
  25. Base. Render (writer );
  26. Writer. Write (@"</Td></Tr><Tr><TdwidthTdwidth= '000000'Colspan= '3'>
    Footer</Td></Tr></Table></Body></Html>");
  27. }
  28. }

The above PageBase. cs is our custom class, so that we can directly inherit the PageBase class on other ASPX pages, rather than System. web. UI. page. The following are indexes. aspx and newContact. the aspx Code contains the index. aspx. cs and newContact. aspx. cs ):

 
 
  1. Index. aspx
  2.  
  3. <% @Pagelanguage= "C #"Codebehind= "Index. aspx. cs"AutoEventWireup=
    "False"Inherits= "Wab. index" %> 
  4. <FormidFormid= "Index"Method= "Post"Runat="Server"> 
  5. <Asp: DataGrididAsp: DataGridid= "Contacts"Runat= "Server"Width=
    "Pixel PX"Height= "Pixel PX"></Asp: DataGrid> 
  6. </Form> 
  7. Index. aspx. cs inherits the custom class PageBase)
  8. Publicclassindex: PageBase
  9. {
  10. ProtectedSystem. Web. UI. WebControls. DataGridcontacts;
  11. PrivatevoidPage_Load (objectsender, System. EventArgse)
  12. {
  13. // Place user code here to initialize the page
  14. }
  15. # RegionWebFormDesignergeneratedcode
  16. OverrideprotectedvoidOnInit (EventArgse)
  17. {
  18. //
  19. // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
  20. //
  21. InitializeComponent ();
  22. Base. OnInit (e );
  23. }
  24. ///<Summary> 
  25. /// The designer supports the required methods-do not use the code editor to modify
  26. /// Content of this method.
  27. ///</Summary> 
  28. PrivatevoidInitializeComponent ()
  29. {
  30. This. Load + = newSystem. EventHandler (this. Page_Load );
  31. }
  32. # Endregion
  33. }
  34. NewContact. aspx
  35. <% @Pagelanguage= "C #"Codebehind= "NewContact. aspx. cs"AutoEventWireup=
    "False"Inherits= "Wab. newContact" %> 
  36. <FormidFormid= "NewContact"Method= "Post"Runat="Server"> 
  37. <P><FONTfaceFONTface="">Name</FONT> 
  38. <Asp: TextBoxidAsp: TextBoxid= "TextBox1"Runat= "Server"></Asp: TextBox></P> 
  39. <P><FONTfaceFONTface="">Last name</FONT> 
  40. <Asp: TextBoxidAsp: TextBoxid= "TextBox2"Runat= "Server"></Asp: TextBox></P> 
  41. <P> 
  42. <Asp: ButtonidAsp: Buttonid= "Button1"Runat= "Server"Text="Button">
    </Asp: Button></P>
     
  43. </Form> 
  44. NewContact. aspx. cs inherits the custom class PageBase)
  45. PublicclassnewContact: PageBase
  46. {
  47. ProtectedSystem. Web. UI. WebControls. TextBoxTextBox1;
  48. ProtectedSystem. Web. UI. WebControls. ButtonButton1;
  49. ProtectedSystem. Web. UI. WebControls. TextBoxTextBox2;
  50. PrivatevoidPage_Load (objectsender, System. EventArgse)
  51. {
  52. }
  53. # RegionWebFormDesignergeneratedcode
  54. OverrideprotectedvoidOnInit (EventArgse)
  55. {
  56. //
  57. // CODEGEN: This call is required by the ASP. NETWeb Form Designer.
  58. //
  59. InitializeComponent ();
  60. Base. OnInit (e );
  61. }
  62. ///<Summary> 
  63. /// The designer supports the required methods-do not use the code editor to modify
  64. /// Content of this method.
  65. ///</Summary> 
  66. PrivatevoidInitializeComponent ()
  67. {
  68. This. Load + = newSystem. EventHandler (this. Page_Load)
  69. }
  70. # Endregion
  71. }

The above is ASP. the basic implementation method of the NET template. However, to do so, the system performance will be reduced a little bit, but this does not affect the actual project, however, I believe that the performance of this system is easy to maintain in the future, and it is worth it.

  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

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.