ArticleDirectory
Master page ------ (Asp. net2.0 New Technology) using masterpage
- Just as theme can be used to format the display mode of various controls on the entire website, the master page can be used to define the same part of different webpages on the website. For example, the whole website has the same pattern, the same page header, the same footer, and the same navigation bar. In this case, you can define these controls on a masterpage. Other webpages only need to inherit this masterpage.
- The write method of the master page file is no different from that of other aspx pages. The main difference is that the master page contains a contentplaceholder, which defines some areas that can be inherited by other pages, controls can be placed on other pages in this area.
Eg: master page file mymaster. Master
<% @ Master language = "VB" codefile = "mymaster. master. VB "inherits =" mymaster "%> <HTML xmlns =" http://www.w3.org/1999/xhtml ">
The master page file can have its own serverCodeFile, which defines a contentplaceholder control to define an adrotator control and its associated data source xmldatasource for the inherited pages. there will be an advertisement below the aspx file of the master!
Format. aspx
<% @ Page Language = "VB" masterpagefile = "~ /Mymaster. master "autoeventwireup =" false "codefile =" frommaster. aspx. VB "inherits =" frommaster "Title =" untitled page "%> <asp: Content ID =" content1 "contentplaceholderid =" contentplaceholder1 "runat =" server "> <asp: label id = "label1" runat = "server" text = "name"> </ASP: Label> <asp: textbox id = "textbox1" runat = "server"> </ASP: textbox> <asp: button id = "button1" runat = "server" text = "Submit"/> <br/> <asp: label id = "label2" runat = "server"> </ASP: Label> </ASP: content>
Notes
- 1. The control status in the *. aspx file cannot be dynamically changed *. Master File
- 2. If theme is defined in the master page, vs2005 will give a warning. The actual operation is based on the theme of the Child page.
- 3. You can configure the master page in Web. config for the whole site.
<Configuration> <system. Web> <pages masterpagefile = "~ /Site. Master "/> </system. Web> </configuration>
- 4. dynamically apply the master page
Void page_preinit (Object sender, eventargs e) {page. masterpagefile = "~ /Site. Master ";}
- 5. Operate controls in the master page (weak type)
<Asp: Label id = "title" runat = "server"/> (Label) master. findcontrol ("title"). Text = "orders ";
- 6. Operate controls in the master page (strong type)
<Asp: Label id = "title" runat = "server"/>... <script language = "C #" runat = "server"> Public String titletext {get {return title. text;} set {Title. TEXT = value ;}</SCRIPT> content page: Add the following statement <% @ mastertype virtualpath = "~ /Tree. master "%> in this way, the Asp.net compiler will generate a strong master attribute for US (in the CS File class of the page file ). in this way, we can directly use the master. to perform operations on the masterpage. call the following Master. titletext = "orders ";
- 7. Respond to master page events
Contentpage responds to the events of some controls on the masterpage (for example, button ).
As mentioned in scenario 1, masterpage buttons can be exposed as attributes,
Subscribe to the click events on the contentpage page. However, the combination of masterpage and contentpage is too high.
A better pattern should be to define the event in app_code, delegate. Define the event in masterpage and expose the event.
In this way, you can subscribe to this event in contentpage.
The master page can be nested. The nested master page can only contain content controls,
However, the content control itself can be nested with the contentplaceholders control.
<! -- Orders. Master --> <% @ master masterpagefile = "~ /Site. master "%> <asp: Content contentplaceholderid = "... "runat =" server "> <asp: contentplaceholder id = "... "runat =" server ">... </ASP: contentplaceholder> <asp: content>