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:
- PageBase. cs
- PublicclassPageBase: System. Web. UI. Page
- {
- PublicstringPageTitle="Test template";
- Protectedoverrisponidrender (System. Web. UI. HtmlTextWriterwriter)
- {
- Writer. Write (@"<Html><Head>
- <Metahttp-equivMetahttp-equiv= 'Content-type'Content= 'Text/html;Charset=Gb2312'>
- <Title>"+ This. PageTitle +"</Title></Head>");
- Writer. Write (@"<Body>
- <TableborderTableborder= '0'Width= '000000'>
- <Tr>
- <TdwidthTdwidth= '000000'Bgcolor= '#006699'Align='Center'><FontcolorFontcolor='
# Ffff'><B><AhrefAhref='Index. aspx'>Homepage</A></B></Font></Td>
- <TdcolspanTdcolspan= '2'Width= '000000'>Advertisement bar</Td>
- </Tr>
- <Tr>
- <TdwidthTdwidth= '000000'Valign= 'Top'>
- <P>Navigation</P>
- <P><AhrefAhref='Newcontact. aspx'>Add contact</A></P>
- <P>Search for contacts</P>
- </Tr>
- <TdwidthTdwidth='10'> </Td>
- <TdwidthTdwidth='123'>
- ");
- Base. Render (writer );
- Writer. Write (@"</Td></Tr><Tr><TdwidthTdwidth= '000000'Colspan= '3'>
Footer</Td></Tr></Table></Body></Html>");
- }
- }
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 ):
- Index. aspx
-
- <% @Pagelanguage= "C #"Codebehind= "Index. aspx. cs"AutoEventWireup=
"False"Inherits= "Wab. index" %>
- <FormidFormid= "Index"Method= "Post"Runat="Server">
- <Asp: DataGrididAsp: DataGridid= "Contacts"Runat= "Server"Width=
"Pixel PX"Height= "Pixel PX"></Asp: DataGrid>
- </Form>
- Index. aspx. cs inherits the custom class PageBase)
- Publicclassindex: PageBase
- {
- ProtectedSystem. Web. UI. WebControls. DataGridcontacts;
- PrivatevoidPage_Load (objectsender, System. EventArgse)
- {
- // Place user code here to initialize the page
- }
- # RegionWebFormDesignergeneratedcode
- 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
- }
- NewContact. aspx
- <% @Pagelanguage= "C #"Codebehind= "NewContact. aspx. cs"AutoEventWireup=
"False"Inherits= "Wab. newContact" %>
- <FormidFormid= "NewContact"Method= "Post"Runat="Server">
- <P><FONTfaceFONTface="">Name</FONT>
- <Asp: TextBoxidAsp: TextBoxid= "TextBox1"Runat= "Server"></Asp: TextBox></P>
- <P><FONTfaceFONTface="">Last name</FONT>
- <Asp: TextBoxidAsp: TextBoxid= "TextBox2"Runat= "Server"></Asp: TextBox></P>
- <P>
- <Asp: ButtonidAsp: Buttonid= "Button1"Runat= "Server"Text="Button">
</Asp: Button></P>
- </Form>
- NewContact. aspx. cs inherits the custom class PageBase)
- PublicclassnewContact: PageBase
- {
- ProtectedSystem. Web. UI. WebControls. TextBoxTextBox1;
- ProtectedSystem. Web. UI. WebControls. ButtonButton1;
- ProtectedSystem. Web. UI. WebControls. TextBoxTextBox2;
- PrivatevoidPage_Load (objectsender, System. EventArgse)
- {
- }
- # RegionWebFormDesignergeneratedcode
- 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
- }
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.
- 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