ASP .NET 2.0: Reference pages
As is known, the majority of sites have the same, standard marking of pages which integral part is the top and bottom page headers and, perhaps, the most important part - a body of page though certainly, there are also exceptions. ASP .NET 2.x offers a unique method of creation of an identical marking for pages of a site is use, so-called, reference pages. If you worked with early versions ASP (ASP, ASP .NET 1.x), probably, remember, that constantly it was necessary to copy a HTML-code of page with elements ASP. Certainly, use of included files (include) in classical ASP, and the user elements of management (*.ascx) in ASP .NET 1.x, considerably simplified development of Web-sites, but it will not be compared to that, that suggests ASP .NET 2.x.
Who they, these Reference pages?
And so, what such reference page? The reference page is a file with expansion *.master, he practically than does not differ from files of aspx-pages already familiar to you. The reference page represents the container for a content and can contain a html-marking of page, html-elements, server elements of management and the user elements of management. In other words, the reference page contains a marking of page, the same the top and bottom page header, the menu of navigation, in general everything, that is actually displayed on all pages of a site, and also the reference page contains one or more fields for substitution of a content (see fig. 1). The content thus is on pages aspx which in turn refer to reference page. Thus, the developer should not duplicate a marking of pages any more as she will be in one file.
Job with reference pages
As I already spoke, the reference page is very similar to pages aspx, but has a number{line} of differences. So, instead of @Page directive, the reference page has @Master directive. @Master directive has the same set of parameters, as well as @Page, most the following parameters often are applied: Language - specifies language on which the code is written; Debug - specifies a status of a mode of debugging; Inherits - contains a classname of a separate code; ClassName - sets a classname, created at processing page; EnabledTheming - defines{determines} a status of use by that; Src - sets a name of an initial file with a class of a separate code.
Here it is necessary to note, that the specified parameters in @Master directive are not inherited by affiliated pages (pages of a content), and used only within the framework of reference page. So for example, argument Language at reference page can matter C *, and at pages of a content - VB.
On reference pages also it is possible to use the same directives, as on pages aspx, for example @Register, @Import, etc.
Each reference page (them can be a little in one project, depending on structure of a site and needs{requirements}, for example, for a forum one reference page, for other part of a site - another) should have at least one element ContentPlaceHolder which actually and is the container for a content. Element ContentPlaceHolder usually empty, but he also can contain a content which will be displayed by default in case on pages aspx there will be no content. The content is in element Content on pages aspx. Managing element Content is a basic element of page of a content and works only together with element ContentPlaceHolder.
The link to reference page for pages of a content can be in @Page directive in parameter MasterPageFile where the way to a file of reference page is underlined, for example:
<% Page Language = "VB" MasterPageFile = "~/MasterPage.master" CodeFile = "Default.aspx.vb" Inherits = " _ Default " %>
, And also, the link to reference page can is in a file web.config, in it sluchaet, the reference page will be applied to all pages aspx, taking place in one folder with web.config (or to the application if web.config is in the root of the application), for example:
<configuration>
<system.web>
<pages masterPageFile = "~/MsterPage.master">
</pages>
</system.web>
</configuration>
Besides it, the reference page can be specified in execution time of the program, for it it is necessary to change property MsterPageFile in processing event Page_PreInit. For example:
Protected Sub Page_Init (ByVal sender As Object, ByVal e As System. EventArgs) Handles Me. Init
Me. MasterPageFile = " ~/NewMasterPage.master "
End Sub
Pay attention, the given code will call a mistake if on page aspx in @Page directive argument MasterPageFile will be specified.
It is necessary to note also, that reference pages can refer to other reference pages, for this purpose it is necessary to specify a way to reference page in argument MasterPageFiledirektivy @Master.
From the theory to practice
And so, perhaps theories enough, we shall pass to practical use of reference pages. For the beginning, we shall make simple reference page, we shall name her{it} MasterPage.master:
<% Master Language = "VB" %>
<! DOCTYPE html PUBLIC " - // W3C // DTD XHTML 1.0 Transitional // EN " " http: // www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns = " http: // www.w3.org/1999/xhtml ">
<head runat = "server">
<title> the Simple example of job with reference pages </title>
</head>
<body>
<form id = "form1" runat = "server">
<div>
<h1> This pattern of design will be on all pages of a content </h1>
<hr/>
<asp:ContentPlaceHolder ID = "ContentPlaceHolder1" runat = "server">
</asp:ContentPlaceHolder>
<hr/>
<div style = " text-align: center ">
Copyrght (c) it is simple koopirajt
</div>
</div>
</form>
</body>
</html>
Pay attention to element ContentPlaceHolder. Presence of this element in reference page is necessary, as I already spoke, he serves as the element - container for a content.
Now, we shall create a little aspx pages with a content which will refer to our reference page. The first page, by developed tradition and standards, will be named Default.aspx
<% Page Language = "VB" MasterPageFile = "~/MasterPage.master" AutoEventWireup = "false" %>
<asp:Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "Server">
Greetings, is a content of the main page! <br/>
<a href = "Second.aspx"> To proceed{pass} to other page </a>
</asp:Content>
Pay attention to argument MasterPageFile in @Page directive which specifies a file of reference page, in our case it is file MasterPage.master which is in the root of the application.
As you can see, the page of a content does not contain tags <html> </html>, <head> </head>, <body> </body> and <form> </form> as these tags already are in our reference page. Pay attention to element Content, in particular his{its} parameter ContentPlaceHolderID - here specifies a name of the container of a content in reference page, i.e. a name of element ContentPlaceHolder into which the content will be inserted. Element Content contains the text of a content, besides the text any allowable elements of aspx-pages here can contain.
Now create one more aspx-page, name her{it} Second:
<% Page Language = "VB" MasterPageFile = "~/MasterPage.master" AutoEventWireup = "false" %>
<asp:Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "Server">
And here at us a content of the second page of page! <br/>
<a href = "Default.aspx"> To proceed{pass} to the main page </a>
</asp:Content>
Now start the project and ASP .NET will connect reference page to page of a content.
Access to properties of reference page
Access to objective model of reference page is possible{probable} through property Master, however if to try to address directly to reference page there will be a mistake. It is connected to that, that the reference page represents the link to object of reference page and gives access only for open properties and methods.
For an example, place on reference page element Label and name it{him} lblTitle. To receive probably to change the text of this element, it is necessary to create corresponding properties in a class of reference page:
Public Property MyTitle () As String
Get
MyTitle = lblTitle. Text
End Get
Set (ByVal value As String)
lblTitle. Text = value
End Set
End Property
Now, you can dynamically change, or receive, the text in an element lblTitle reference page, for example, at processing event of loading of aspx-page:
Protected Sub Page_Load (ByVal sender As Object, ByVal e As System. EventArgs) Handles Me. Load
CType (Master, MasterPage) .MyTitle = " Greetings! "
End Sub
Actually and everything if at you will arise what or questions, write, I shall try to answer.

|