Home
Home Page
And again about functional programming on Python
Where the Python has come crawling?
Practical application OOP in PHP5
Functional programming in language Python
Approaches of language Python - an amusing example of optimization
Use ext/mysqli: Part I - the Review and the prepared expressions
How to receive the maximal result from registration in catalogues
XML-RPC In language Python
The API-specification of databases of language Python, version 2.0
Programming of the Web-client in language Python
Useful advice for job with XML
Future Web - behind semantics
Whether it is necessary to cancel spaces of names XML?
Use AJAX in ASP.NET
ASP .NET 2.0: Reference pages
Patterns of registration
What is ASP.NET. Installation and the test project.
Anatomy ASP.NET. ASP.NET in operation.
Server elements of management Continuation.
Links
 
topic



Patterns of registration

Before to start use of functionalities of the web - application, the user puts{folds} the attitude{relation} to the application, being based on appearance of pages, deciding{solving} for itself, whether the arrangement of elements management and whether rjabit at him{it} in eyes from design izyskov is convenient for him{it}. Therefore the competent design of the interface is hardly probable less important, functionalities.


The problem{task} of the programmer will consist in a spelling of a program code, and the artist - designer in creation HTML of imposition. Thus in the big projects often it is required, that job above a code and shala in parallel above a code and above HTML performance of the same page.


In this chapter{head} as the demonstration application and an example of use of new opportunities ASP.NET 2.0 the web - application for conducting diaries (blogov) will be created. In examples of this clause{article} the skeleton of design of the similar application will be created.



Patterns of registration in ASP and ASP.NET 1.x


In classical ASP for performance of a similar problem{task} there was a standard method with use of Include directive of the web - server. For example, the general{common} features of design of pages of a website are allocated, and HTML the code of a cap and the bottom part of page is located in files header.html and footer.html, accordingly. Thus for all dynamic pages the pattern which includes Include directives and ways to corresponding files with HTML a marking is created.



<% LANGUAGE = VBScript %>

  <% Option Explicit of %>

  <! - *include virtual = "include/header.html"->

  <! - *include virtual = "include/footer.html"->


The program code and HTML the performance, specific to each concrete page is located between include directives.


This approach allowed to separate most part HTML of performance from a code of program logic and to solve a problem of distribution of job between the programmer and the designer. But the given approach has two significant lacks. First, HTML tags, can be open in an included file, and are closed in the basic file, that practically completely excludes support of visual editing. Second, in the given script it is impossible to adjust under object-oriented model and, thus, at attempt to apply the same model to advantages of object-oriented programming ASP.NET are lost.


In ASP.NET 1.x the general{common} elements of registration include in the user elements of management which then register for use in a code of performance of each page with use of Register directive.


The above mentioned example can be adapted for ASP.NET 1.x with use of the user elements of management Header.ascx and Footer.ascx. Then the code of a pattern of page will look as follows.



<% Page %>

  <% Register TagPrefix = "Design" TagName = "Header" Src = "~/Controls/Header.ascx" %>

  <% Register TagPrefix = "Design" TagName = "Footer" Src = "~/Controls/Footer.ascx" %>

  <Design:Header runat = "server" ID = "Header"/>

  <Design:Footer runat = "server" ID = "Footer"/>


This approach reaches{achieves} the basic purpose - the code of program logic can be changed not mentioning patterns of a cap and the bottom part of page, and at change of a code or performance of the user elements, they inure for all pages of the web - application. Besides convenience of this approach that the page is easy for transforming to the user element of management, only having replaced Page directive, on Control directive, and also, in the given approach opportunities of object-oriented programming are not lost.


But, it is necessary to note and existing lacks. At modification in the name of methods and properties of an element of management it is necessary to make manually changes to all pages using this element of management so anew to compile assembly of the application. Also, change of a code of pages will call recompilation of dynamically created classes of pages on the server.


Besides it, in this approach the problem with division opening and closing tegov between different elements of management if it is necessary that elements of management created the interface of all page as a whole is saved.

Patterns of registration in ASP.NET 2.0


In ASP.NET 2.0 for support of patterns of registration the new technology named Master Pages is created. This approach is close to the approach with use of the user elements of management, but comprises essential advantages. For the developer there is no necessity to transform page in the user elements of management more, that essentially simplifies and accelerates development and debugging. Job of the designer also essentially becomes simpler, as he can easy work with code HTML of performance, having switched on in him{it} a little bit ASP.NET the elements realizing technology Master Pages.

Architecture of a pattern of registration


The pattern of registration in ASP.NET 2.0 represents a file containing HTML performances and elements of management ASP.NET 2.0, determining appearance one or several (and it is possible also all) pages of the web - application. The pattern of registration on the structure is identical to page, but instead of Page directive Master directive is used, and also in a pattern of registration special elements of management ContentPlaceholder are used. The pages using a pattern of registration, elements of management Content and all marking specific to page should contain should be placed in these elements of management. It is necessary to note, that accommodation of a code of a marking or elements of management outside of elements Content in the page using patterns of registration, will lead to to a mistake of time of compilation.


At search to the page using a pattern of registration, there will be a replacement of elements of management ContentPlaceholder of a pattern with the code of performance generated by page. This process is submitted in a Fig. 1.



Structure of a pattern and page


Let's consider a code of performance of the elementary pattern of registration and the elementary page using the given pattern. These the pattern and page realize an example resulted by consideration of use of patterns of registration in ASP and ASP.NET 1.x. Here a cap and the bottom part of page it is switched on in a file of a pattern.


File Master.master



<% Master %>

  <html>

          <head runat = "server">

                <title> It is the Pattern </title>

          </head>

          <body>

                <form runat = "server" ID = "frmMain">

                  <asp:ContentPlaceHolder runat = "server" ID = "PageContent">

                  </asp:contentplaceholder>

                </form>

                <p> Copyrght © 2006 GM </p>

          </body>

  </html>

[/CDOE] 


File Default.aspx



  <% Page MasterPageFile = "~/Master.master" Title = " It is page " %>

  <asp:Content runat = "server" ContentPlaceHolderID = "PageContent">

    <h1> Hello world! </h1>

  </asp:content>


At the reference{manipulation} to page Default.aspx the environment of performance ASP.NET 2.0 will execute association of page and a pattern, and resulting HTML will look as follows.



<html>

          <head runat = "server">

                  <title> It is the Pattern </title>

          </head>

          <body>

                  <form runat = "server" ID = "frmMain">

                          <h1> Hello world! </h1

                  </form>

                  <p> Copyrght © 2006 GM </p>

          </body>

  </html>


This elementary example shows the general{common} principle of job of a pattern of registration, but already from this example advantages of patterns of registration in ASP.NET 2.0 are obvious: objective orientation, an opportunity to divide{share} a code of a pattern and a code of page, an opportunity of support in the visual editor. Besides it it is necessary to mention, that ASP.NET 2.0 supports the enclosed patterns of registration and gives convenient mechanisms for purpose{appointment} of patterns for a plenty of pages. About it it will be written further in this chapter{head}, by detailed consideration of interaction of patterns of registration and pages.



The mechanism of association of a pattern and page


At the reference{manipulation} to the page using a pattern of registration, the environment of performance ASP.NET checks presence of a file of a pattern and builds hierarchy of patterns if the enclosed patterns of registration are used. The information on dependences enters the name in a file with expansion .compiled in a directory for time files ASP.NET. The provisional structure of a file for an above mentioned example is resulted below.



<? xml version = " 1.0" encoding = "utf-8"?>

  <preserve resultType = "3" virtualPath = "/TestWeb/Default.aspx " hash = "fffffff5007efec4" filehash = "6029956f826994b1" flags = "110000" assembly = "App_Web_whm_iyzn" type = "ASP.default_aspx">

      <filedeps>

          <filedep name = "/TestWeb/Default.aspx "/>

          <filedep name = "/TestWeb/Default.aspx.cs "/>

          <filedep name = "/TestWeb/Master.master "/>

          <filedep name = "/TestWeb/Master.master.cs "/>

      </filedeps>

  </preserve>


Then the environment of performance creates a dynamic class for a pattern of registration. This class also can be found in a directory for time files ASP.NET.



public partial class Master: System. Web. UI.MasterPage

  {

      protected void Page_Load (object sender, EventArgs e)

      {

}

}


The class of a pattern inherits to class System. Web. UI.MasterPage which, in turn, is a derivative of class UserControl.


If the file of a pattern contains any elements of management at generation of a class corresponding properties will be added. For example, for the elements of management Form determined in an example and ContentPlaceholder.



protected global:: System. Web. UI.WebControls. ContentPlaceHolder PageContent;

  protected global:: System. Web. UI.HtmlControls. HtmlForm frmMain;


Besides it, the class will be expanded at the expense of addition of properties, events and the methods necessary for realization of own functionality ASP.NET. For use of contents of the pages using a pattern, properties such as ITemplate are added. With which further the code will be appropriated{given} by generated by a class of page.



private System. Web. UI.ITemplate __ Template_PageContent;


The same as and for any page ASP.NET, for a class of a pattern the code creating copies of elements of the management used in a pattern will be generated. We shall look after this process by the example of an element of management PageContent for which the environment of performance had been created the same property. In the beginning the copy of this element of management is created.



global:: System. Web. UI.WebControls. ContentPlaceHolder __ ctrl;

  __ ctrl = new global:: System. Web. UI.WebControls. ContentPlaceHolder ();

  this. PageContent = __ ctrl;

  __ ctrl. ID = "PageContent";


Then with the help of a call of method InstantiateIn determined in interface ITemplate elements of management, certain{determined} on the page using a pattern, are added in element ContentPlaceHolder.



if ((this. ContentTemplates! = null))

  {

  this.@__Template_PageContent = ((System. Web. UI.ITemplate) (this. ContentTemplates ["PageContent"]));

}

  if ((this.@__Template_PageContent! = null))

  {

  this. __ Template_PageContent. InstantiateIn (__ ctrl);

}

  else

  {

  System. Web. UI.IParserAccessor __ parser = ((System. Web. UI.IParserAccessor) (__ ctrl));

  __ parser. AddParsedSubObject (new 

  System. Web. UI.LiteralControl ("rn"));

}


The problem{task} of method InstantiateIn will be to add elements of management in the container. For example, the elementary realization of a method can be such:



public void InstantiateIn (Control ctrl)

  {

      Button btn = new Button ();

      btn. Click + = new EventHandler (this. Button_Click);

      ctrl. Controls. Add (btn);

}


Thus, the pattern is filled by contents of elements Content determined in ASP.NET page. Comparison of contents of elements Content is made on set property ContentPlaceHolderID. And as a result the structure of the received object can be presented the following diagram:


From the above mentioned description and the diagram, it is obvious, what not the pattern includes page, and the page includes a pattern, elements of management ContentPlaceholder which are filled by the elements of management determined in page. It is possible to track analogy to a technique of the organization of patterns to the help of elements of management as the pattern of registration in ASP.NET 2.0 is, inherently, an element of management with the expanded functionality. The only thing from a technique with use of the user elements of management will be, that element Form is defined{determined} in a file of a pattern of registration.


From all described above, follows, that at use of patterns of registration life cycle of page will not change. The order of events Init and Load pages and a pattern will be same, as well as for any user element of management: event Init for a pattern arises earlier, than for page, and Load on the contrary, all over again for page, then for a pattern.



Creation of a pattern of registration


Having on hands ready imposition of the general{common} design of the web - application it is possible to start creation of patterns of registration. First of all, it is necessary to allocate all repeating elements of registration of pages, such as caps, the bottom parts of pages, static menus and similar fragments of appearance of pages.


To create the basic pattern of registration of the application we shall be defined{determined} with an arrangement of active parts on page and we shall allocate blocks which will be the general{common} for all pages.


It is logical to assume, that areas "Cap", "Copyrights" and "Navigation" in an operating time of the application will be exposed to minor alterations and it is possible to allocate them into a pattern of registration.


In the field of active contents we place an element of management ContentPlaceHolder and we set his{its} contents by default.



<asp:ContentPlaceHolder runat = "server" ID = "PageContent">

  <p> No content available. </p>

  </asp:ContentPlaceHolder>


As the pattern is an element of management his{its} properties can be expanded programmno, as well as any other element of management. In a file of program logic of a pattern the code of corresponding properties then it is possible to get access to them in the page using a pattern is added.



private int _UserID = 0;

  private string _BlogTitle = " The OpenBlog ";

  public string BlogTitle

  {

    get {return _BlogTitle;}

    set {_BlogTitle = value;}

}

  public int UserID

  {

    set {_UserID = value;}

}


To address to it of property in the page using a pattern property Master of object Page is used. But, thus, it is necessary to remember necessity of performance of reduction object Master:



((Themes_MasterPages_DefaultMaster) Master) .UserID = UserID;



The enclosed patterns


It is necessary to note, that Master directive has the same attributes, as Page directive: is allowable to specify attribute MasterPage and, thus, to create hierarchical structure of the enclosed patterns of registration. For this purpose in a parental pattern the element of management ContentPlaceHolder, in an affiliated pattern element Content which, in turn can contain elements ContentPlaceHolder also is created. However, it is necessary to note, that editor Visual Studio 2005 does not support visual editing of the enclosed patterns.


Use of patterns of registration


As already it has been said above that the page approached{suited} for a pattern, she should contain an element of management Content with established property ContentPlaceHolderID. But associirovat` concrete page with a pattern of registration it is possible in several ways.


The first way has been used in an example above and will consist in the direct instruction{indication} of a way to a file of a pattern in attribute MasterPageFile of Page directive.



<% Page Language = " C * " MasterPageFile = "~/Themes/DefaultMaster.master" %>


The second way consists in purpose{appointment} of a pattern of registration at once to all pages in the certain directory or for the web - application in a file of a configuration of the application web.config.



<system.web>

        <pages masterPageFile = "~/Themes/Master.master"/>

  </system.web>


It is necessary to pay attention, that if the pattern of registration is established in a file web.config directories or the application all ASP.NET pages in a directory or in all the application, accordingly, should contain elements Content. The page can contain less elements Content, than elements ContentPlaceHolder contained in a pattern, but the pages containing elements of management or a code of a marking outside of elements Content will throw out exception. Also it is necessary to remember, that in the application configured thus it will be impossible to add in the future usual page, e using patterns.


The third way of linkage of page and pattern consists in dynamic giving value to property MasterPageFile directly in a code. For change of global parameters of page, the code should be executed prior to the beginning of processing page, therefore to establish property MasterPageFile it is possible only in obrabotchike Page_PreInit. Attempt to change value MasterPageFile in obrabotchikakh Page_Init and Page_Load will lead to to ejection of exception.



Page. MasterPageFile = " ~/Themes/Master2.master ";


However if attribute MasterPageFile of Page directive attempt to execute an above mentioned code will lead to to exception as Page directive is processed prior to the beginning of performance of a code of page is established.



The conclusion


In given clause{article} it has been told about technology of patterns of registration ASP.NET 2.0 which has come in the stead of use of the user elements of management. In ASP.NET 2.0 division of a code and performance becomes simple and pleasant employment{occupation} as practically all job is incured with the environment of performance.