【原】MasterPage FAQ 【系列一】

由于最近比较忙,所以不能经常写博客了,先总结 11 条共大伙参考,等下周有时间,再补充补充,目前就暂且叫它【MasterPage FAQ 系列一】吧

1.       If set the EnableViewState property on the content page to true but set the same property to false in the master page, which will be effective?

MasterPage.  Because setting on the master page will take priority.

 

2.       Can apply an ASP.NET theme to a master page directly?

No.  If add a theme attribute to the @Mater directive, the page will raise an error when it runs.  

 

3.    Can configure master page in web.config file?

Yes. At the application level By making a setting in the pages element of the application's configuration file (Web.config), we can specify that all ASP.NET pages (.aspx files) in the application automatically bind to a master page, see code:

 

<pages masterPageFile="~/MasterPage.master">

 

4.    Why the content which defined in master page is disappeared at run time?

Because of

 

5.    Master page cycle?

a)      Master page controls Init event

b)     Content controls Init event

c)      Master page Init event

d)     Content page Init event

e)      Content page Load event

f)       Master page Load event

g)     Content controls Load event

h)     Content page PreRender

i)       Master page PreRender

j)        Master page controls PreRender

k)       Content controls PreRender event

 

6.    How to get control which on the master page from the content page which inherits from the master page?

We can use the following code to get it,

Master page inline code:

 

    <form id="form1" runat="server">

    <asp:Label ID="lblMessage" runat="server" Text="I am a MasterPage"></asp:Label>

    <div>

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

        </asp:ContentPlaceHolder>

    </div>

</form>

 

Content page behind code:

 

(Page.Master.FindControl("lblMsg") as Label).Text = "I Love ASP.NET!";

 

7.    How to use a strong type between the master page and content page?

We can place a @ MasterType directive in our content page to do this.

For example, see the following code which in master page,

Inline code:

 

    <form id="form1" runat="server">

    <asp:Label ID="lblMessage" runat="server" Text="I am a MasterPage"></asp:Label>

    <div>

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

        </asp:ContentPlaceHolder>

    </div>

</form>

 

Behind code:

 

    public string Message

    {

        get { return lblMessage.Text; }

        set { lblMessage.Text = value; }

}

 

Content page,

Inline code:

 

<%@ Page Title="" Language="C#" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>

 

Behind code:

 

Master.Message = "I Love ASP.NET!";

 

Note:

We need set this line in Web.config file if we use above code,

 

<pages masterPageFile="~/MasterPage.master">

 

We must use the following inline code in the content page if we do not assign any master page in the Web.config,

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>

 

8.    How to add the Meta data from content page dynamically?

HtmlMeta metaTag = new HtmlMeta();

metaTag.Name = "keywords";

metaTag.Content = "ASP.NET,C#,VB";

Header.Controls.Add(metaTag);

 

Now run the application and open the source code in the clint side, we will see the bellow line,

 

<meta name="keywords" content="ASP.NET,C#,VB" />

 

9.    How to add CSS or JavaScript in from content page dynamically?

Page class contains a public property named Header.  We can use it access to the head tag as a server side control if the set runat=”server” for the head tag which in the master page.  Now see the code,

 

    HtmlLink cssLink = new HtmlLink();

    cssLink.Href = "~/css.css";

     cssLink.Attributes.Add("rel", "stylesheet");

     cssLink.Attributes.Add("type", "text/css");

Header.Controls.Add(cssLink);

 

HtmlLink jsLink = new HtmlLink();

jsLink.Href = "~/js.js";

jsLink.Attributes.Add("language", "javascript");

jsLink.Attributes.Add("type", "text/javascript");

Header.Controls.Add(jsLink);

 

Now run the application and open the source code in the clint side, we will see the bellow two lines,

 

    <link href="css.css" rel="stylesheet" type="text/css" />

<link href="js.js" language="javascript" type="text/javascript" />

 

Note:

CSS means Cascading Style Sheets

 

10.  How to change the master page at run time?

We can use the following code to do this,

protected void Page_PreInit(object sender, EventArgs e)

{

if (Session["user"] == null)

this.Page.MasterPageFile = "~/MasterPage2.master";

else

this.Page.MasterPageFile = "~/MasterPage1.master";

}

Note:

We must put the code in Page_PreInit who will be executing just before the page render event.

 

11.  Can master page be nested?  How to do? [refer : http://msdn.microsoft.com/en-us/library/x2b3ktt7.aspx]

Yes. Master page can be nested, see the whole code,

parent.master code:

 

<% @ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

    1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="Form1" runat="server">

    <div>

        <h1>

            Parent Master</h1>

        <p style="font: color=red">

            This is parent master content.</p>

        <asp:ContentPlaceHolder ID="MainContent" runat="server" />

    </div>

    </form>

</body>

</html>

 

child.master code,

 

<%@ Master Language="C#" MasterPageFile="~/parent .master" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <asp:Panel runat="server" ID="panelMain" BackColor="lightyellow">

        <h2>

            Child master</h2>

        <asp:Panel runat="server" ID="panel1" BackColor="lightblue">

            <p>

                This is child master content.</p>

            <asp:ContentPlaceHolder ID="ChildContent1" runat="server" />

        </asp:Panel>

        <asp:Panel runat="server" ID="panel2" BackColor="pink">

            <p>

                This is child master content.</p>

            <asp:ContentPlaceHolder ID="ChildContent2" runat="server" />

        </asp:Panel>

        <br />

    </asp:Panel>

</asp:Content>

 

child.aspx code:

 

<%@ Page Language="C#" MasterPageFile="~/child.master" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" runat="server">

    <asp:label runat="server" id="Label1" text="Child label1" font-bold="true" />

    <br />

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ChildContent2" runat="server">

    <asp:label runat="server" id="Label2" text="Child label2" font-bold="true" />

</asp:Content>

posted @ 2009-02-11 02:03  海洋——海纳百川,有容乃大.  阅读(1392)  评论(9)    收藏  举报