I rarely saw Chinese information about introducing ASP.NET Page Templates (hereinafter referred to as Page Template), I have seen a lot of information in foreign sites. I don't know if you are not happy. Use page template. When I develop ASP.NET web applications, I prefer to use it, and the effect is not bad. Today, some information is compiled and sorted out, talk about ASP.NET Page Templates.
When you are developing a part or all pages having a common element, such as banner, copyright statement, navigation bar, etc .; even part or all pages have functions, such as authentication session judgment, error capture display, data Operation and help prompt function, you will encounter such a problem: How to make these ASP.NET pages have these common elements and features simple and convenient?
In previous ASP programming, use include Contains files to solve the above problem. Although the include file has solved the shared page element problem to a certain extent, it is tight between the containment file and the included page, which means that your page must contain and contain the corresponding header file in a certain order, otherwise the program will Do not run normally. At the same time, this tight coupling relationship allows you to modify each header file and each ASP page in order to modify a significant interface or important feature of the site in the future.
Now, in ASP.NET, many of the new features and object-oriented objects can be used to find better solutions to solve this problem.
Therefore, the main discussion will be: ASP.NET What channels can I solve the shared problem of page elements or functions? What is the effect of ASP.NET PAGE TEMPLATE?
First, what channels can ASP.NET to solve the shared problem of page elements or features?
1, User Controls
When I just contacted asp.net, I saw a book of Taiwanese, and they introduced something about Pagelet (they called "web accessories", which felt fresh. Later, the real name of Pagelet is called User. Control, we call user controls. User control is actually a small page module that encapsulates HTML code block and rear server-side code to make it easy to reuse it in many different ASP.NET pages. This section may also More familiar, there is not much to say, more specific
Http://chs.gotdotnet.com/quickstart/aspplus/doc/webpagelets.aspx. User controls still do not have a problem that the page elements and functions are shared. For example, you encapsulate a header.ascx to allow each page head to keep consistent, and that you have to pull the header.ascx in a certain location; when you need to change the Header.ascx location, When you put under each page, you must also modify each page.
2, Page Template
In the ASP.NET Web application project, all ASPX pages are inherited from the System.Web.ui.page class. Because of this, in the Page Template scheme, in order to solve the sharing of views and functions, we created a page base class inherited from the System.Web.ui.page class, and inherit all ASPX pages in the web application Page base class PageBase.
As can be seen from the above figure, a layer of PageBase will help us add some public views or features to PageBase to make The page has public characteristics. Below is PageBase class code: use
System;
Using
System.Web.ui;
public
Class Pagebase: System.Web.ui.page
{
Private string_pagetiple;
Public String PageTitle
{
Get {return _pagetility}
Set {_pagetitle = value;
}
Protected Override Void Render (HTMLTextWriter Writer)
{
//
First create HTML and Body nodes
Writer.write (@ "
hEAD>
//
Secondly allow the base class to present HTML code and ASP.NET controls in the ASPX page.
Base.render (Writer);
//
Finally end Body and HTML nodes
Writer.write (@ "
body>
html> ");
}
}
Leave an ASPX page WebForm1.aspx only left <% @ page ... ..%> instructions and