We have learned the basic usage of MasterPages In the last time, but we will also find that such a template cannot meet the requirements.
The first problem I encountered was the page title.
For a Web form using a template, how should the page title be written? Generally, the TITLE of a page is specified by the TITLE tag of the standard HTML element, which exists in the HEAD tag. Currently, only the template control has complete HTML tags. Other forms only reference these common HTML elements through MasterPages. Where should the page title be written?
I first added a region in the template's <title> </title> and added its own page title in the corresponding content of the form.
Template code:
<% @ Register TagPrefix = "mp" namespace = "Microsoft. Web. Samples. MasterPages" assembly = "MasterPages" %>
<Html>
<Head>
<Title>
<Mp: region id = "regTitle" runat = "server"> </mp: region>
</Title>
</Head>
<Body>
...
</Body>
</Html>
The result shows that MasterPages does not correctly output the regTitle content as the page title, but displays a <div> MARK containing the page title content at the beginning of the page result. I guess MasterPages cannot output the region in
So can we define a client script function that sets the title in the public part of the template, and write it in the template's background code to call the client script function on the client, output the content of the corresponding content in the form of the application template to the parameter of this function, so as to dynamically implement the function of outputting the page title for the form?
The author adds a hidden region in the <body> of the template, because the corresponding content does not need to be output to the page result and is displayed by the client.
<Body>
<Mp: region id = "regTitle" runat = "server"> </mp: region>
...
</Body>
At the same time, a client script function is defined in <Head>
...
<Script language = javascript>
Function setTitle (title)
{
Do *** ent. title = title;
}
</Script>
</Head>
The following code is added to the Page_Load event Method of the template UserControl:
If (regTitle. Controls. Count> 0)
{
System. web. UI. literalControl title = (System. web. UI. literalControl) regTitle. controls [0]. controls [0]; // MasterPages puts the content in LiteralControl into a Panel, and then puts the Panel into region.
This. Response. Write ("<script language = javascript> setTitle ('" + title. Text + "') </script> ");
}
Apply the form of this template and add the <mp: content> of id = "regTitle" to its <mp: contentcontainer> to define the page title:
<% @ Register TagPrefix = "mp" namespace = "Microsoft. Web. Samples. MasterPages" assembly = "MasterPages" %>
<Mp: contentcontainer runat = "server" MasterPageFile = "~ /Template. ascx "id =" Test ">
<Mp: content id = "regTitle" runat = "server"> Test page title </mp: content>
<Mp: content id = "regContent" runat = "server"> Form content test </mp: content>
</Mp: contentcontainer>
In the next article "MasterPages Practical Tips", you can learn another MasterPages-based template front-end control technique-automatically generate the global client script of the page itself.
Hope to help you. Thank you!