Page multiplexing method based on ASP.NET

xiaoxiao2021-03-06  49

Abstract: this paper Introduces Some Methods of Reusable Web Page Which Combining with the Features of ASNET, AND TAKING A Web Page of A News Web Site As an Example. Keywords: ASP.NET Double User Control Inheritment Key Words: ASP .NET Reusable User Controls Inherit 0, introduction With the continuous development of the network, the web-based B / S architecture is the mainstream of the current application, in which business logic and database are placed in server segments, users are browsing The server operates the data of the server. 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. The problem proposes a website, especially the website that combines with the database (such as news websites, product introduction websites, etc.), there are many web pages, different only data related to the database (such as different news content, different Product, 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, navigation bar as an example. The sample page is as follows: 2. Solution ASP.NET introduces the concept of web form user controls, using it we can easily create customized 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, the content is as follows: <% @ Control%>

"IMG alt =" "src =" image / banner1.jpg "border =" 0 "> footer is two lines of text , Implement the form of two lines.

New files FOOTER.ASCX, the content is as follows: <% @ Control%>

www.hahaha.com < / TD>
Hahaha Studio
Contact US: imcwj@126.com Navigation menu has 5 links, respectively corresponds to campus, academic report, respectively , Current affairs, sports, science education five columns, corresponding links to Campus.aspx, Dissertation.aspx, News.aspx, Sports.aspx, Education.aspx, the way the picture link is used. 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 also ASP.NET One of the benefits.

Create documents UnivNewsBase.cs, 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) {// build the 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") ; // Get the FORM in the web page, insert the control established above into the 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);}}} code Using the loadControl method to load the defined web user control, Forced 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. Extend the above solution For the horizontal navigation menu, now there are now many cases, the navigation menu is vertical arrangement, located on the left side of the page (as shown in Figure 3), which cannot be used in this way.

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

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