Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:21:57+00:00 2026-05-31T02:21:57+00:00

Is it possible to access Masterpage variables from a web user control that is

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:21:58+00:00Added an answer on May 31, 2026 at 2:21 am

    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)
        {
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

IS it possible to access web content directly from the (Safari) toolbar? I can
Is it possible to access an element on a Master page from the page
Is it possible to access hardware devices (web-cams, magnetic card readers, etc.) from within
Is it possible to access Flash's ExternalInterface from QWebView ? If so, how can
Is it possible to access content of user settings.xml file from a maven plugin
Can we access an element of a content page from master page using JavaScript?
Is it possible to access object properties that can only be accessed with the
Is it possible to access the data belonging to the apps that ship with
Is it possible to access a parent member in a child class... class MainClass
Possible Duplicate: Access return value from Thread.Start()'s delegate function public string sayHello(string name) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.