Asp.net中内容页访问模板页控件及属性

一、一层嵌套:只有一个模板页

模板页里面现在有一个TextBox控件,内容页想要拿到这个TextBox的Text属性值,需要在内容页做相应的逻辑判断处理:

模板页前台代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="txtMsgTest" runat="server" Text="Hello"></asp:TextBox>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
         
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

模板页后台代码没有写任何逻辑,

内容页前台代码没有任何处理,

内容页后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ContextPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string strTmp = string.Empty;
        try
        {
            strTmp = ((TextBox)Master.FindControl("txtMsgTest")).Text;
            Response.Write("Got masterPage text value " + strTmp + " of control  txtMsgText ");
        }
        catch
        {
            Response.Write("Pls check  TextBox`s  name or Id  then try it ");
            return;

        }
    


    }
    //protected void Page_LoadComplete(object sender,EventArgs e)
    //{
    //    string strTmp = string.Empty;
    //    strTmp = ((TextBox)Master.FindControl("txtMsgTest")).Text;
    //    Response.Write("Got masterPage text value " + strTmp + " of control  txtMsgText ");
    //}
}

我们可以用Page的Master属性的FindControl方法直接获得想要获得控件的属性值,这里注意一层嵌套的话可以在内容页的Page_load事件里写获得的代码,但是二层嵌套的话不能在这里写获得的代码,否则会报错(没有创建引用实例),一层嵌套也可以把获得的代码写在Page_LoadComplete事件里面。

输出效果如下:

 

另外这里还需要注意:内容页的Page_Load是在模板页的Page_Load之前执行的,可以加断点看下

二、直接把模板页里面的对象封装,以属性的形式封装

Master前台代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:TextBox ID="txtMsgTest" runat="server" Text="Hello"></asp:TextBox>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
         
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

Master的后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write("who is first ");
    }

    public TextBox txtTest
    {
        get
        {
            return this.txtMsgTest;
        }
        set
        {
            this.txtMsgTest = value;
        }
    }
}

内容页前台:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ContextPage.aspx.cs" Inherits="ContextPage" %>
<%@MasterType VirtualPath="~/MasterPage.master"  %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

就多了第二句话,意思是说:使用MasterType指令获取母版页控件的引用,通过使用MasterType,可以创建与母版页的强类型引用。

内容也后台:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ContextPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

       Response.Write( Master.txtTest.Text);
//就可以通过属性的方式访问了
  
    }

}

 

效果图:

相应的这里我们可以看到,用属性封装的方式就可以访问到模板页里面一些字段的值,不光能访问控件。

三、二层模板页嵌套

现在最开始有一个模板页MasterPage  然后新建另一个模板页,他选择模板页的时候选择第一个模板页masterPage2,然后再新建一个普通的WebPage给他选择模板页选择第二个模板页(MasterPage2),现在在模板页2里面有一个TextBox控件,我们想做的就是在内容页拿到这个控件的Text属性的值然后打印:

MasterPage1前台:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<!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></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
         
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

后台没有任何代码,

MasterPage2的前台代码:

<%@ Master Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:TextBox ID="TextBox1" runat="server" Text="这个是第二个模板页控件"></asp:TextBox>
  <asp:ContentPlaceHolder id="ContentPlaceHolderContextPage" runat="server">    
        </asp:ContentPlaceHolder>
</asp:Content>

注意:这里的TextBox控件一定要放在<asp:ContentPlaceHolder的外面,不然会获取不到,

Masterpage2后太没有任何代码:

内容页前台:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="TwoMasterPage.aspx.cs" Inherits="TwoMasterPage" %>

<%-- Add content controls here --%>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderContextPage" Runat="Server">
</asp:Content>

 

内容页后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TwoMasterPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ContentPlaceHolder strCPH = (ContentPlaceHolder)Master.Master.FindControl("ContentPlaceHolder1");
        string str = ((TextBox)strCPH.FindControl("TextBox1")).Text;
        Response.Write(str);
    }

    //protected void Page_LoadComplete(object sender, EventArgs e)
    //{
    //    ContentPlaceHolder strCPH = (ContentPlaceHolder)Master.Master.FindControl("ContentPlaceHolder1");
    //    string str = ((TextBox)strCPH.FindControl("TextBox1")).Text;
    //   Response.Write(str);
    //}
}

注意:虽然这个例子内容页在Page_Load事件中能运行成功,但是我们还要多加留意,往往我们是在MasterPage里面运行到某个时候然后就给相应的字段赋值,然后才在内容页用这样的方法获取,写在Page_Load就不行了,应为我们的内容页的PageLoad会先于MastPage的PageLoad,所以在这种场景下就要把获取的代码写在内容页的Page_LoadComplete事件里面。

 

运行效果:

 

 

有了两个的嵌套三层的思路和二层的处理办法是一样的,无非就是Master.Master.Master 。

两层的嵌套时内容页也可以访问第一层模板页的内容就是直接Master.Master.FindControl("YourControlName").

 

小弟浅见,如果那个地方有问题,还请各位大侠指正下。

 

参考的链接:http://blog.csdn.net/sunboy_2050/article/details/5270936

      http://book.51cto.com/art/200810/92202.htm

 

 

 

posted @ 2012-06-13 21:43  st_gloden  阅读(2028)  评论(0编辑  收藏  举报