1

How can I make a control that is being added to MasterPages only? or simply how can I know if the control is added to MasterPage aspx or is added to an ordinary WebForm that inherits from System.Web.UI.Page?

I want to put control in master page like this:

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


But it should not accept to be used in an ordinary WebForm like this

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


Thanks.

1 Answers1

0

To access it from a web page you need to reference to the master page.

Here is how you do it.

Master Page

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

Master Page Code Behind

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

    }
}

One page base on this master page

please note this reference : <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %> you need it so the page can now the class of the user control.

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

and the code behind

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";
    }
}

And the user control

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

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

and the code behind

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

    }
}

Some similar answers:
ASP.NET how to access public properties?

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • This is not what I'm asking about, first it is okay thanks, the question is to throw an exception in case the control is used in a WebForm not on a MasterPage. – Abdelfattah Ragab Aug 03 '15 at 09:10
  • @AbdelfattahRagab You can use this code to addapt it to your question. The moment you can see how to access the control you can simple add an 'if' to see if exist. I demostrate here how to access the control... do the rest is not so hard. – Aristos Aug 03 '15 at 09:25