Question

Is it possible to access Masterpage variables from a web user control that is in a child page? I know you can access them in child pages by adding the following

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

It doesn't seem to work when trying to access from a Web control that is inside a child page

Was it helpful?

Solution

User Controls essentially should be unaware of any pages outside the control. The better approach would be to have the control expose properties and events that the page itself (master page or normal) will use to set and retrieve values. Take this simple example:

    class PassValueEventArgs : EventArgs
    {
        public string Value { get; set; }
    }

    public event EventHandler<PassValueEventArgs> RequestingValue;

    public void ControlDoingWork()
    {
        PassValueEventArgs e = new PassValueEventArgs();
        if (RequestingValue != null)
        {
            RequestingValue(this, e);
        }
        string fromHandlingPage = "Received " + e.Value + " from a handling page.";
    }

Then whenever the user control should have a value, the page containing the user control can just handle the RequestingValue event and send the value to the user control. Otherwise just expose a public property of the user control, which you can even make databound, for an even easier solution.
Adding a complete example of the event-driven approach:
WebUserControl1EventArgs.cs

public class WebUserControl1EventArgs : EventArgs
{
    public double ValueToSquare { get; set; }
}

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplicationCS1_net20.WebUserControl1" %>

Text below will display "Nothing passed from parent page." if the event is unhandled,
else will display the square of the number passed if handled.<br /><br />
<asp:Label runat="server" ID="Label1" Font-Bold="true" Font-Size="Larger" Text="Nothing passed from parent page."></asp:Label>

WebUserControl1.ascx.cs

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public event EventHandler<WebUserControl1EventArgs> RequestingNumber;

    protected void Page_Load(object sender, EventArgs e)
    {
        ControlDoingWork();
    }

    private void ControlDoingWork()
    {
        if (RequestingNumber != null)
        {
            WebUserControl1EventArgs e = new WebUserControl1EventArgs();
            RequestingNumber(this, e);
            Label1.Text = (e.ValueToSquare * e.ValueToSquare).ToString();
        }
    }
}

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm1" %>

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

<!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>
        <uc1:WebUserControl1 ID="WebUserControl11" runat="server" 
            OnRequestingNumber="WebUserControl11_RequestingNumber" />
    </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

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

    }

    protected void WebUserControl11_RequestingNumber(object sender, WebUserControl1EventArgs e)
    {
        e.ValueToSquare = 3.3;
    }
}

WebForm2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm2" %>

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

<!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>
        <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
    </div>
    </form>
</body>
</html>

WebForm2.aspx.cs

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

    }
}

OTHER TIPS

Use the Page's Master property to access to it's masterpage. After that you can you FindControl method or use the master's public properties if it have any. For example in the master page code behind:

public Label Title { get { return lblTitle; } }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top