2

I have a MasterPage (MyBoxx.Master) referencing 2 usercontrols :

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MyBoxx.master.cs" Inherits="MyBoxxMaster" %>
<%@ Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
<%@ Register TagPrefix="uc1" TagName="Footer" Src="Footer.ascx" %>

My user control "Header" contains among other things a searchbox. I want to hide this searchbox when visiting some pages. Therefore I added a boolean property to my user control and use this property when rendering the usercontrol to determinate whether to display the search box or not :

public partial class uxHeader : System.Web.UI.UserControl
{

    bool _showSearch = true;
    public bool ShowSearch
    {
        get { return _showSearch; }
        set { _showSearch = value; }
    }
    [...]
    protected void Page_Load(object sender, EventArgs e)
    {
        [...]
        searchBox.Visible = _showSearch;

    }
}

I then try to access this "ShowSearch" Property from the content page :

((uxHeader)Page.Master.FindControl("Header1")).ShowSearch = false;

Problem is I get the following error when trying to compile :

Error   15  The type or namespace name 'uxHeader' could not be found (are you missing a using directive or an assembly reference?)

The thing is I'm sure I got it to work and compile at some point as it works on the previously released production version. But now I'm doing a change to something else in the same site, and can't compile anymore.

From various post on SO, I tried adding the following lines to my content page aspx :

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

Without any success ! I saw also some answers about the page Lifecycle, but this can't be the problem here as I'm getting an error on compilation, not a bug upon execution.

If anyone has any advice on how I can fix this for good, I would grandly appreciate.

Thanks !

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • Is this part `(uxHeader)Page.Master.FindControl` located in code-behind or in aspx file? Try using fully-qualified name for this class - will it help? Do you got compilation error during build in visual studio, or during compilation by ASP.Net in browser? Do all classes (master page, page, control) share the same assembly? – Lanorkin Apr 02 '13 at 12:20
  • This code is located in codebehind. I got compilation error during build. They all have the same assembly. I'm actually working at a possible solution and will of course post an answer if I ever succeed... – Laurent S. Apr 02 '13 at 12:22
  • Ok, then you need `using my.namespace.here;` or `(my.namespace.here.uxHeader)Page.Master.FindControl` – Lanorkin Apr 02 '13 at 12:29
  • I would use a config file... – Cobra_Fast Apr 16 '13 at 14:16

2 Answers2

3

Well, I found several working solutions... and I think I understood how/why it worked earlier

1) it seems that compilation has a role to play in this. If I comment the line, compile the site, and then try to add the line again, the type uxHeader is "available" in VS and I can compile the site back again with the line uncommented...

2) As first solution is obviously not a long-term solution, I found that referencing the user-control (without actually using it of course) in the content page aspx would do the trick :

<%@ Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>

3) I also tried this one, which I find the cleanest... In the master page, expose a public property :

public uxHeader PageHeader
{
    get
    {
        return Header1;//Header1 is the id of the userControl dropped in masterpage
    }
}

In the content page aspx, I then put :

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

then, still in the content page, but in the codebehind, and after a compilation of the site, I can use :

this.Master.PageHeader.ShowSearch = false;

Hope this will help the ones searching for help on the subject in the future. I see this is a recurent question

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • Will you help to access the Usercontrol from Content Page. In Master Page: In Content Page: ShoppingCartControl myWCtl = this.Master.FindControl("myCtl") as ShoppingCartControl; It give error : "the type or namespace does not exist! any idea? – SHEKHAR SHETE Feb 21 '14 at 06:17
  • Did you try the solution here above ? If yes and the problem persists, better open your own question with a complete description of the problem and what you've tried to solve it – Laurent S. Feb 21 '14 at 10:46
1

Depending on how you have your User Control coded you may or may not be able to access all it's properties/methods when exposing it to the master page as a master page property..

Here is a solution that works:

In your master page you need to register your user control (.ascx) and place it on the master within the form tag.

Register the User Control

<%@ Register Src="~/Controls/MyUserControl.ascx" TagPrefix="uc" TagName="MyUserControl" %>

Add the User Control to the Master Page

<form id="frmMain" runat="server">
        <uc:MyUserControl runat="server" ID="ucMyUserControl" />
        <div id="main-wrapper">
            <div id="main">...

Now for the content page, you must create a reference in each of the content pages that use the master page for which you want to use the control.

Add the Reference in the content Page

<%@ Reference Control="~/Controls/MyUserControl.ascx" %>

Now you can setup a public variable at the page level and access it's properties/methods

Partial Class MyPage

  Inherits System.Web.UI.Page

  Public usrCtrl As MyUserControl


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Master.FindControl("ucMyUserControl") IsNot Nothing Then
        usrCtrl = CType(Master.FindControl("ucMyUserControl"), MyUserControl)
        usrCtrl.ExecMyMethod()
    End If
    ...
eyegropram
  • 672
  • 4
  • 11