A method of Web page reuse based on ASP.net

Source: Internet
Author: User
Tags contains count header server memory visual studio
Asp.net| Web page Abstract:this paper introduces some methods of reusable web page which combining with the features of ASP.net, and taking a Web page of a News Web site as an example.

Keyword: asp.net reuse user control inheritance

Key Words:ASP.NET Reusable User Controls inherit

0. Introduction

With the development of the network, the B/s architecture based on the web is the mainstream of the current application, in which the business logic and database are placed in the server segment, and the user operates the server-side data through the browser. Before the Microsoft.NET platform has not been launched, people can achieve the above goal by ASP, now you can choose ASP.net.

ASP.net is a compiled, based. NET environment, can be used with any. NET-compatible languages (including Visual Basic.NET, C #, and JScript.NET.) Authoring applications. Any ASP.net application can use the entire. NET Framework. ASP, by contrast, is an interpretive programming framework for VBScript and JavaScript, which are limited in functionality and require components written in C + +, Java, and other languages to extend their functionality, combined with interpretation and running with limited efficiency.



1. Questions raised

A website, especially a website that combines with a database (such as a news site, a product introduction site, etc.), has many pages that are similar, and different only data related to the database (such as different news content, different products, etc.). We do not need to write a file for each page, the price is too high, we can fix the invariant parts, the change in part according to the different client requests dynamically generated.

Take an example of a news site page that contains a header, footer, navigation bar, and the sample pages are as follows:





2. Solution method

Asp. NET introduces the concept of Web Forms user control, which makes it easy to create custom reusable controls. User controls can be compiled and stored in server memory on the first request, which can shorten the response time for subsequent requests. The user control inherits from System.Web.UI.UserControl. We can make a user control of headers, footers, and navigation bars.

For simplicity, the header contains only a picture that points to the home page. Create a new file Header.ascx, which reads as follows:

<%@ Control%>

<table width= "775" border= "0" >

<tr>

&LT;TD align= "center" valign= "middle" ><a href= ". /default.aspx "></a>

</td>

</tr>

</table>

The footer is two lines of text and is implemented in a two-row column table. Create a new file Footer.ascx, which reads as follows:

<%@ Control%>

<table width= "775" border= "0" >

<tr>

&LT;TD align= "center" valign= "Middle" >www.hahaha.com</td>

</tr>

<tr>

&LT;TD align= "center" valign= "middle" >hahaha Studio </td>

</tr>

<tr>

&LT;TD align= "center" valign= "Middle" >contact us: <a href= "mailto:imcwj@126.com" >imcwj@126.com</a> </td>

</tr>

</table>

Navigation menu has 5 links, respectively corresponding to the campus, academic reports, current affairs, sports, science and education five columns, corresponding links for campus.aspx, dissertation.aspx, news.aspx, sports.aspx, Education.aspx, the way to use the picture link. Create a Menu.ascx file with the same code as the header, omitted here.

The effect of three controls is shown in Figure 1.

We can use the above control in five files such as campus.aspx, take compus.aspx as an example. First add the statement to the beginning of the file <%@ register tagprefix= "uc1" tagname= "header" src= "Controls/header.ascx"%> register the control, which registers the header control, Also register the footer and navigation menu controls, and then use the page location you want, using the phrase <uc1:header id= "Header1" runat= "server" ></uc1:header> join the above registered controls. You can also drag and drop a Web Forms user control to the desired location directly in the Visual Studio.NET development environment, and look at the Web page code to see that the two methods are the same. The picture in the development environment is shown in Figure 2.



However, each page to manually add the previous paragraph of the code, seems to be a bit of trouble, whether you can set a base class, and the specific page as a subclass to inherit it, it turns out to be feasible, which is also one of the benefits of asp.net.

Create a file UnivNewsBase.cs with the following code:

Using System;

Using System.IO;

Using System.Web.UI;

Using System.Web.UI.HtmlControls;

Using Univnews.controls;



Namespace Univnewsbase

{

public class UnivNewsBase:System.Web.UI.Page

{

protected override void Render (HtmlTextWriter writer)

{

Create a header, navigation menu, footer three controls

Header Header = (header) LoadControl (Request.applicationpath +

Path.altdirectoryseparatorchar +

"Controls/header.ascx");

Menu menu = (menu) LoadControl (Request.applicationpath +

Path.altdirectoryseparatorchar +

"Controls/menu.ascx");

Footer Footer = (Footer) LoadControl (Request.applicationpath +

Path.altdirectoryseparatorchar +

"Controls/footer.ascx");



To get the form in the Web page, insert the control you created above

Control form = Page.controls[1];

Form. Controls.addat (0, header);

Form. Controls.addat (1, menu);

Form. Controls.addat (Page.controls[1]. Controls.Count, footer);



Callback Parent class method

Base. Render (writer);

}

}

}

Use the LoadControl method in your code to load a defined Web user control and then cast it to the appropriate type. Page.controls[1] Gets the form of the Web page and then uses the AddAt method of the form's Controls property to add controls to the form. It is worth noting that page.controls[1 is used when adding footer control footer. Controls.Count to locate, so you can ensure that the footer control is the last position on the form.

By deleting the Web forms user control in the campus.aspx file, the base class of the campus class is changed from System.Web.UI.Page to Univnewsbase in Campus.aspx.cs, and the rest remains unchanged, and the results of the runtime are found to be the same as before the modification. and other pages, usually with the same layout as campus.aspx, can inherit from Univnewsbase. Thus, the production of these pages is greatly simplified, and if the layout is to be changed, the base class can be modified, which makes full use of the advantages of object-oriented methods.



3, extended

The above solution is for the horizontal navigation menu, but there are many cases where the navigation menu is vertically arranged, located on the left side of the page (Figure 3), and the above method is not available. Generally, you need to make a basic page frame (not the concept of frame in HTML), put the header, footer, navigation menu, and then on the right side of the navigation menu, that is, depending on the page different and different parts of the content, put on such as placeholder, DataList and so on server-side controls, and then in the corresponding codebehind files in the page, manipulate the data that these controls should contain, such as binding news headlines from a database to DataList, or displaying specific news. In this way, the paging file (. aspx) can also do as little as possible and the rest of the transaction is handled on the server side.



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.