ASP.NET MasterPage只能控制?

时间:2015-08-02 03:21:49

标签: asp.net

如何制作仅添加到MasterPages的控件?或者我怎么知道控件是否被添加到MasterPage aspx或者是否被添加到从System.Web.UI.Page继承的普通WebForm?

我想将控件放在母版页中,如下所示:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="SkolaControlsWorkings.Site1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:MasterPageOnlyControl runat="server" />
    </div>
    </form>
</body>
</html>


但它不应该接受在像这样的普通WebForm中使用

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="SkolaControlsWorkings.WebForm3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">
    <cc1:MasterPageOnlyControl runat="server" />
</asp:Content>


感谢。

1 个答案:

答案 0 :(得分:0)

要从网页访问它,您需要引用母版页。

这是你如何做到的。

母版页

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

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

<!DOCTYPE html>

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

        <uc1:WebUserControl ID="WebUserControl1" runat="server" />

        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

母版页代码背后

public partial class Dokimes_so_MasterPage : System.Web.UI.MasterPage
{
    // here we expose the control to public
    public Dokimes_so_WebUserControl getCustomControl
    {
        get
        {
            return WebUserControl1;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

此主页上的一页

请注意此参考:<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>您需要它,以便页面现在可以是用户控件的类。

<%@ Page Title="" Language="C#" MasterPageFile="~/Dokimes/so/MasterPage.master" AutoEventWireup="true" CodeFile="GetControl.aspx.cs" Inherits="Dokimes_so_GetControl" %>


<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

和背后的代码

public partial class Dokimes_so_GetControl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // first you get the upper "master"
        // then you get the custom control
        // then you access a property inside the custom control
        ((Dokimes_so_MasterPage)Master).getCustomControl.cSetText = "test";
    }
}

用户控件

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Dokimes_so_WebUserControl" %>

<asp:Literal runat="server" ID="txtTest"></asp:Literal>

和背后的代码

public partial class Dokimes_so_WebUserControl : System.Web.UI.UserControl
{
    public string cSetText
    {
        set
        {
            txtTest.Text = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

一些类似的答案:
ASP.NET how to access public properties?