0

I have a user control on the master page and I would like to pass in a value into that user control from the subpage, how would I be able to pass the values?

This control is in the master page

<%@ Register TagPrefix="test" TagName="Data" Src="controls/TEST.ascx" %>

This code variable is within the user control

public partial class Controls_TEST : System.Web.UI.UserControl
{
    private string _Title;
    public string Title
    {
        get { return _Title; }
        set { _Title = value; }
    }
}

Code within the subpage

public partial class sub_page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         Controls_Test m = LoadControl("~/Controls/TEST.ascx");
         m.Title = "TEST";
    }
}

Note the sample code within subpage does not work because it cannot find that user control within the subpage.

I've tried Page.Master.FindControl and it also does not work for me. PLease help.

Anna
  • 239
  • 1
  • 7
  • 21

1 Answers1

4

Use properties to communicate from your Page to your MasterPage and use properties to communicate from your MasterPage to the UserControl.

To get a reference to the control in your MasterPage you should provide a public property that returns it:

For example(in MasterPage):

public Controls_Test MyControl
{
     get
     {
         return Controls_TEST1;
     }
}

And you can call this property from one of your ContentPages in this way(f.e. if your master's type is named "SiteMaster"):

protected void Page_Load(object sender, EventArgs e)
{
    ((SiteMaster)Page.Master).MyControl.Title = "TEST";
}

As a rule of thumb: the more you encapsulate your controls, the more robust ,failsafe, maintanable and extendable your code will be.

Hence it would be better to provide only access to the Title rather than to the whole UserControl.

In MasterPage:

public String Title
{
     get
     {
         return Controls_TEST1.Title;
     }
    set
    {
        Controls_TEST1.Title = value;
    }
}

In the ContentPage:

((SiteMaster)Page.Master).Title = "TEST";

On this way you could change the logic and controls in your UserControl and MasterPage without having problems in your pages that already have accessed the UserControl directly.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • +1 for mentioning exposing only the value and not the control itself so the MasterPage implementation can change without breaking the subpages. – adam0101 Mar 09 '12 at 21:15
  • I am randomly receiving this error using your method, please help: Unable to cast object of type 'ASP.master_twocolumn_searchresults_master' to type 'TwoColumn_SearchResults'. – Anna Jun 21 '12 at 15:51
  • @Anna: Where do you get this exception and when? What is `TwoColumn_SearchResults`? – Tim Schmelter Jun 21 '12 at 16:15
  • http://voices.yahoo.com/aspnet-masterpage-casting-problem-5806152.html Attached is an article about MasterPage casting – Anna Jun 21 '12 at 16:26
  • TwoColumn_SearchResults is the name of my MasterPage in the code behind: public partial class TwoColumn_SearchResults : System.Web.UI.MasterPage The exception gets called randomly and the exception gets called when it reaches this line of code: ((TwoColumn_SearchResults)Page.Master).Title = year + " " + make + " " + model + " "; – Anna Jun 21 '12 at 16:28
  • I've changed the casting to: ((ASP.master_twocolumn_searchresults_master)Page.Master).Title = year + " " + make + " " + model + " "; But I'm not sure if that exception will fire again if the site does not compile properly. – Anna Jun 21 '12 at 16:31
  • Name of the master page file is: Master_TwoColumn_SearchResults.master, if that helps you – Anna Jun 21 '12 at 16:33
  • 1
    @Anna: So you think this is a Microsoft Bug? Then just restart IIS(or the Application Pool) or edit web.config to recompile all. I don't see where it's related to this question. You might want to ask another question with a simplified sample. **Edit** Wait, heres already a question on SO: http://stackoverflow.com/questions/512064/unable-to-cast-object-of-type-x-to-type-x-asp-net [Here's](http://tinyurl.com/3bnhzt) another useful article. – Tim Schmelter Jun 21 '12 at 16:47
  • I was hoping you may know something about this error with your method and it'll be easy to reference if there is a solution. Thanks for your quick reply! =D – Anna Jun 21 '12 at 17:06
  • Sir can you please help me on this question:http://stackoverflow.com/questions/35220489/how-to-find-label-control-from-content-page-which-is-inside-master-page-of-user – I Love Stackoverflow Feb 05 '16 at 10:20