在Master母版页中通过FindControl()方法获取服务器控件

一、服务器控件与代码都位于母版页

1、Master母版页

(1)Html代码

<%@ 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>

</head>

<body>

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

    <div>

        <table style="width: 734px">

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_01.jpg" /></td>

            </tr>

            <tr>

                <td>

                    <asp:TextBox ID="TextBox1" runat="server" BackColor="#E0E0E0">母版页文本框文本内容</asp:TextBox>

                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="母版页按钮" /></td>

            </tr>

            <tr>

                <td>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

            </asp:contentplaceholder>

                </td>

            </tr>

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_btm.jpg" /></td>

            </tr>

        </table>

        <br />

        &nbsp;</div>

    </form>

</body>

</html>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write("<script language=javascript>window.alert('" + TextBox1.Text + "')</script>");

        TextBox tb1 = (TextBox)FindControl("TextBox1");

        Response.Write("<script language=javascript>window.alert('FindControl()获取:" + tb1.Text + "')</script>");

 

        /*以下用法错误:

         *TextBox tb2 = (TextBox)Master.FindControl("TextBox1");

         *Response.Write("<script language=javascript>window.alert('" + tb2.Text + "')</script>");

         */

    }

}

 

2、Content内容页

(1)Html代码

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="MasterPage.master" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat=server>

    &nbsp;

</asp:Content>

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

}

 

二、服务器控件与代码都位于内容页

1、Master母版页

(1)Html代码

<%@ 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>

</head>

<body>

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

    <div>

        <table style="width: 734px">

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_01.jpg" /></td>

            </tr>

            <tr>

                <td>

                    </td>

            </tr>

            <tr>

                <td>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

            </asp:contentplaceholder>

                </td>

            </tr>

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_btm.jpg" /></td>

            </tr>

        </table>

        <br />

        &nbsp;</div>

    </form>

</body>

</html>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

}

 

2、Content内容页

(1)Html代码

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="MasterPage.master" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat=server>

    <asp:TextBox ID="TextBox1" runat="server">内容页文本框内容</asp:TextBox>

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="内容页按钮" />

</asp:Content>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write("<script language=javascript>window.alert('" + TextBox1.Text + "')</script>");

        ContentPlaceHolder cpl1 = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

        TextBox tb1 = (TextBox)cpl1.FindControl("TextBox1");

        Response.Write("<script language=javascript>window.alert('Master.FindControl()获取:" + tb1.Text + "')</script>");

        //或者:

        ContentPlaceHolder cpl2 = (ContentPlaceHolder)FindControl("ContentPlaceHolder1");

        //在此FindControl()代表Master.FindControl(),在Master母版页中查找控件。

        TextBox tb2= (TextBox)cpl1.FindControl("TextBox1");

        Response.Write("<script language=javascript>window.alert('FindControl()获取:" + tb2.Text + "')</script>");

 

        /*以下用法错误:

        *TextBox tb1 = (TextBox)FindControl("TextBox1");

        //在此FindControl()代表Master.FindControl(),在Master母版页中查找控件,而TextBox1在Content内容页中,所以找不到TextBox1控件

        *Response.Write("<script language=javascript>window.alert('" + tb1.Text + "')</script>");

        */

    }

}

 

三、服务器控件位于母版页,代码位于内容页

1、Master母版页

(1)Html代码

<%@ 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>

</head>

<body>

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

    <div>

        <table style="width: 734px">

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_01.jpg" /></td>

            </tr>

            <tr>

                <td>

                    &nbsp;<asp:TextBox ID="TextBox1" runat="server" BackColor="#E0E0E0">母版页文本框内容</asp:TextBox></td>

            </tr>

            <tr>

                <td>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

            </asp:contentplaceholder>

                </td>

            </tr>

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_btm.jpg" /></td>

            </tr>

        </table>

        <br />

        &nbsp;</div>

    </form>

</body>

</html>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

}

 

2、Content内容页

(1)Html代码

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="MasterPage.master" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat=server>

    &nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="内容页按钮" />

</asp:Content>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        TextBox tb1 = (TextBox)Master.FindControl("TextBox1");

        Response.Write("<script language=javascript>window.alert('" + tb1.Text + "')</script>");

    }

}

 

四、服务器控件位于内容页,代码位于母版页

1、Master母版页

(1)Html代码

<%@ 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>

</head>

<body>

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

    <div>

        <table style="width: 734px">

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_01.jpg" /></td>

            </tr>

            <tr>

                <td>

                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="母版页按钮" /></td>

            </tr>

            <tr>

                <td>

        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

            </asp:contentplaceholder>

                </td>

            </tr>

            <tr>

                <td>

                    <img src="../img/biaotou_fin3_btm.jpg" /></td>

            </tr>

        </table>

        <br />

        &nbsp;</div>

    </form>

</body>

</html>

 

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        TextBox tb = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");

        Response.Write("<script language=javascript>window.alert('" + tb.Text + "')</script>");

 

        /*错误的用法:

         * Response.Write("<script language=javascript>window.alert('" + TextBox1.Text + "')</script>");

         */

    }

}

 

2、Content内容页

(1)Html代码

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="MasterPage.master" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat=server>

    &nbsp;<asp:TextBox ID="TextBox1" runat="server">内容页文本框内的文本</asp:TextBox>

</asp:Content>

(2)C#代码

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wygyhm/archive/2008/09/09/2906285.aspx

posted @ 2010-05-17 16:22  过期的风子  阅读(499)  评论(0编辑  收藏  举报