ASP. NET template implementation (C #)

Source: Internet
Author: User
Preface

In the past (or now), when we were working on WEB projects, we often used some shared files, such as the top (header), left (navigation), bottom (footer) of the entire page) wait for some common HTML, ASP, and other files. Even so, we also need to make a framework style for these files in advance (usually using table ?! A few will use DIV) to combine (include) these files to get a complete page, the combination of these shared files, suppose 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.

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 (that is, methods are also supported ). 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 & ccedil; 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, users can customize HTML code (For details, refer to Web User Controls). In this way, the prototype of the solution is coming out. See.

The "Custom class" in the figure is equivalent to an intermediate layer that we add. The custom class inherits the base class System. Web. UI. Page. The code for the custom class is as follows:

PageBase. cs

Program code: public class PageBase: System. Web. UI. Page

{

Public string PageTitle = "test template ";

Protected override void Render (System. Web. UI. HtmlTextWriter writer)

{

Writer. Write (@ "

<Meta http-equiv = 'content-type' Content = 'text/html; charset = gb2312 '>

<Title> "+ this. PageTitle +" </title>

Writer. Write (@ "<body>

<Table border = '0' width = '000000'>

<Tr>

<Td width = '000000' bgcolor = '#160' align = 'center'> <font color = '# ffff'> <B> <a href = 'index. aspx '> homepage </a> </B> </font> </td>

<Td colspan = '2' width = '000000'> advertisement bar </td>

</Tr>

<Tr>

<Td width = '000000' valign = 'top'>

<P> navigation </p>

<P> <a href = 'newcontact. aspx '> Add a contact </a> </p>

<P> Search for contacts </p>

</Td>

<Td width = '10'> </td>

<Td width = '000000'>

");

Base. Render (writer );

Writer. write (@ "</td> </tr> <td width = '000000' colspan = '3'> footer </td> </tr> </table> </body>

}

}

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 (including index. aspx. cs and newContact. aspx. cs ):

Index. aspx

Program code: <% @ Page language = "c #" Codebehind = "index. aspx. cs" AutoEventWireup = "false" Inherits = "wab. index" %>

<Form id = "index" method = "post" runat = "server">

<AspataGrid id = "contacts" runat = "server" Width = "pixel PX" Height = "pixel PX"> </aspataGrid>

</Form>

Index. aspx. cs (inherits the custom class PageBase)

Program code: public class index: PageBase

{

Protected System. Web. UI. WebControls. DataGrid contacts;

Private void Page_Load (object sender, System. EventArgs e)

{

// Place user code here to initialize the page

}

# Region Web Form Designer generated code

Override protected void OnInit (EventArgs e)

{

//

// CODEGEN: This call is required by the ASP. NET Web 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>

Private void InitializeComponent ()

{

This. Load + = new System. EventHandler (this. Page_Load );

}

# Endregion

}

NewContact. aspx

Program code: <% @ Page language = "c #" Codebehind = "newContact. aspx. cs" AutoEventWireup = "false" Inherits = "wab. newContact" %>

<Form id = "newContact" method = "post" runat = "server">

<P> <FONT face = ""> name </FONT>

<Asp: TextBox id = "TextBox1" runat = "server"> </asp: TextBox> </P>

<P> <FONT face = ""> last name </FONT>

<Asp: TextBox id = "TextBox2" runat = "server"> </asp: TextBox> </P>

<P>

<Asp: Button id = "Button1" runat = "server" Text = "Button"> </asp: Button> </P>

</Form>

NewContact. aspx. cs (inheriting the custom PageBase class)

Public class newContact: PageBase

{

Protected System. Web. UI. WebControls. TextBox TextBox1;

Protected System. Web. UI. WebControls. Button Button1;

Protected System. Web. UI. WebControls. TextBox TextBox2;

Private void Page_Load (object sender, System. EventArgs e)

{

}

# Region Web Form Designer generated code

Override protected void OnInit (EventArgs e)

{

//

// CODEGEN: This call is required by the ASP. NET Web 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>

Private void InitializeComponent ()

{

This. Load + = new System. EventHandler (this. Page_Load );

}

# Endregion

}

The above is ASP. the basic implementation method of the template in. NET. However, this will reduce the system performance slightly, but it 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.

Windows 2000 server + Visual Studio.net (. NET Framework 1.0) passed the test.

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.