Page multiplexing method based on ASP.NET

xiaoxiao2021-03-06  81

Summary: Take a news website page as an example, combined with the ASP.NET characteristics, the method of multiplexing the web page is introduced.

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.

Keywords: ASP.NET multiplex user control inheritance

Key Words: ASP.NET Reusable User Controls Inherit

0 Preface

With the continuous development of the network, the web-based B / S architecture is the mainstream of the current application, where business logic and databases are placed in such architectures, and the user operates the server's data through the browser. Before the Microsoft.Net platform has not been launched, people can achieve the above objects through the ASP, and now you can choose ASP.NET.

ASP.NET is a compiled, based .NET-based environment that creates applications with any .NET compatible language (including Visual Basic.net, C #, and JScript.net.). Any ASP.NET application can use the entire .NET Framework. In contrast, the ASP is an interpreted programming framework. The scripting language is VBScript and JavaScript. These two languages ​​are limited. They need to use components written in languages ​​such as C , Java to extend their functions, and the interpretation is running , Limited efficiency.

1. Problem

A website, especially those combined with the database, such as news websites, product introduction websites, etc.), there are many pages similar, different only data related to the database (such as different news content, different products, etc.) . We don't have to write a file for each page, the cost of doing is too high, we can fix the constant part, and the changed part is dynamically generated according to the client request.

Take a news website page containing header, footer, and navigation bar. The sample page is as follows:

2, solution

The concept of web form user controls is introduced in ASP.NET, which uses it to easily create custom reusable controls. The user control can be compiled and stored in the server memory during the first request, which can shorten the response time of the subsequent request. User control inherits from System.Web.ui.userControl. We can make headers, footers, and navigation columns into user controls.

For simplicity, the header only contains a picture pointing to the home page. The new file Header.ascx is as follows:

<% @ Control%>

Footers are two lines of text, implemented with a list of two lines. New files FOOTER.ASCX, the content is as follows:

<% @ Control%>

www.haha.com

Hahaha Studio

Contact US: imcwj@126.com

The navigation menu has 5 links, respectively corresponds to the five columns of campus, academic reports, current affairs, sports, and science and education. Corresponding to Campus.aspx, Dissertation.aspx, News.aspx, Sports.aspx, Education.aspx, The way the picture link is made. Establish a menu.ascx file, which is similar to the header, which is omitted.

The effect of the three controls is shown in Figure 1.

We can use the above controls in five files such as Campus.aspx to take compus.aspx as an example. First, add the statement in the file to the statement <% @ register tagprefix = "uc1" tagname = "header" src = "controls / header.ascx"%> registration control, the statement is registered with header control, the same registration footer and navigation Menu control; then use the webpage location to use the statement to join the above registered control, it is possible. You can also drag and drop the web form user control directly in the Visual Studio.NET development environment. You can see the web code that the effect of the two methods is the same. The picture in the development environment is shown in Figure 2.

However, each page must be manually coupled with the above-mentioned code, it seems to be a bit trouble, can set a base class, and the specific page is used as a subclass to inherit it, the fact proves is feasible, this is the ASP.NET band One of the benefits.

Establish a file univnewsbase.cs, the code is as follows:

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)

{

// Establish a header, navigation menu, and 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");

/ / Get the form in the web page, insert the control established 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;

// Toned parent method

Base.render (Writer);

}

}

}

Use the loadControl method in the code to get the defined web user control, and then force to convert to the corresponding type. Page.Controls [1] Get the form of FORM, and use the AddAT method of the form's Controls property to add controls to the Form. It is worth noting that when adding pages control FOOTER, uses page.controls [1] .controls.count to locate, so that the last position of the footer control is on the Form.

Delete the web form user control in the campus.aspx file, change the base class of the Campus class by system.Web.ui.page in campus.aspx.cs to univnewsbase, the rest remains unchanged, can be found in runtime results Like the modification. Other pages, all layouts like Campus.aspx can be inherited from UnivneWSBase. Thus, the production of these pages is greatly simplified, and the layout must change, the modified base class is possible, which takes advantage of the advantages of object-oriented methods.

3, expand

The above solution is directed to the horizontal navigation menu, but now there is now a vertical arrangement, located on the left side of the page (as shown in Figure 3), then the above method cannot be used. Generally, it is necessary to make a basic page frame (not the concept of frame in HTML), put the header, footer, navigation menu, and then different from the right side of the navigation menu, depending on the page. Content section, put on server-side controls such as PlaceHolder, DataList, and then in the codeBehind file corresponding to the page, such as binding the news header to Datalist from the database, or Display the specific news, etc. In this way, the page file (.aspx) can also do as little as possible, and the rest of the transactions are processed on the server.

references

[1] Daniel Cazzulino, etc. C # web application entry classic [M]. Beijing: Tsinghua University Press, 2003-08

[2] Huang Zhongcheng ASP.NET Component Design [M] Beijing: Electronic Industry Press, 2004-05

[3] Joseph Mayo C # Technical Insider [M] Beijing: Machinery Industry Press, 2003-01

转载请注明原文地址:https://www.9cbs.com/read-98495.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.041, SQL: 9