views:

1633

answers:

4

I have a UserControl which I have added to my web.config

<add tagPrefix="BCF" src="~/controls/MyMessageBox.ascx" tagName="error"/>

and added to my master page

<BCF:error ID="BCError" runat="server" Visible="false" />

Now I need to be able to reference this control AND its public properties from all child pages that use that masterpage. I did this is my BasePage OnLoad event

public UserControl BCError;
BCError = (UserControl)Master.FindControl("BCError");

Problem is, although I can do this in the .aspx page

BCError.Visible = true;

I cannot reference any of the Controls properties I have put in? Such as ShowError .. If I do

BCError.ShowError = "Error Message";

I just get an error saying

'System.Web.UI.UserControl' does not contain a definition for 'ShowInfo' and no extension method 'ShowInfo'

Can you please point me in the right direction!

This is the code for the user control... I can use the properties in the masterpage code behind (And in a page if I put the control directly into it) but cannot use them in the child page code behind?? It doesn't even show the properties or wrapper methods in the intellisense?

    using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class MyMessageBox : System.Web.UI.UserControl
{
    #region Properties
    public bool ShowCloseButton { get; set; }

    #endregion

    #region Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ShowCloseButton)
            CloseButton.Attributes.Add("onclick", "document.getElementById('" + MessageBox.ClientID + "').style.display = 'none'");
    }
    #endregion

    #region Wrapper methods
    public void ShowError(string message)
    {
        Show(MessageType.Error, message);
    }

    public void ShowInfo(string message)
    {
        Show(MessageType.Info, message);
    }

    public void ShowSuccess(string message)
    {
        Show(MessageType.Success, message);
    }

    public void ShowWarning(string message)
    {
        Show(MessageType.Warning, message);
    } 
    #endregion

    #region Show control
    public void Show(MessageType messageType, string message)
    {
        CloseButton.Visible = ShowCloseButton;
        litMessage.Text = message;

        MessageBox.CssClass = messageType.ToString().ToLower();
        this.Visible = true;
    } 
    #endregion

    #region Enum
    public enum MessageType
    {
        Error = 1,
        Info = 2,
        Success = 3,
        Warning = 4
    } 
    #endregion
}
A: 

Ok I think I reproduced roughly what your describing and I deleted my original answer cause it was way off.

What I found is that when you want a content page to reference a user control being used in a master page and the control is accesible and what not, you will get an error indicating that you need to reference a specific assembly, and then you get errors indicating that no Method exists of type such and such.

By adding the Register page directive on the child page to the user control resolved this issue. I reproduced this even with the control defined in the web.config or on the page. In both cases I still had to explicitly add a Register on the content page.

This doesn't make sense to me but it allowed my code to compile and work. Give it a shot let me know.

Once you do this you can reference the control like

this.Master.MessageBox.ShowInfo();

This assumes that you have a public property called MessageBox on the Master Page.

Edit

I've also found that this works much better if you register the control on both the master and the content page and not use the web.config.

Edit

If you don't want your child page to reference the user control your other option is expose methods on the master page like ShowInfo() which would delegate to the user control.

JoshBerke
Thanks for replying, but how do I reference the masterpage? Or will the usercontrol become visible in the child pages automatically in my intellisense?
leen3o
Thhanks I have added the code.. I'm mystified.. As per the above, the control works perfectly in the masterpage code behind AND if I put thje control directly into a page.. But trying to reference it in the masterpage from a child page it just doesn't recognise the public properties/wrapper methods?? Any ideas would be very appreciated..
leen3o
Thank you for your help Josh.. I thought it wasn't me being stupid!! Having to register the UC on each page defeats the object of having it in the masterpage, I might as well just create the UC in each page to be honest - Its probably less code :S Weird!
leen3o
Yes it is weird, I'd still create it in the master page...all your talking about on the content pages is a one line directive <%@Register src=".." etc...
JoshBerke
I also wonder if this would behave different with a Web App project vs a Web Site. I'm sure it would. Im using a web site model to test this on.
JoshBerke
The problem is I'll have src="" on everypage.. Say I changed the .ascx file? Unlikely I know but I'd have to go through every page to change it :( ... Might try the web app project as I'm also using a web site - Thanks
leen3o
Well find and replace works great for changing a paths. Probally easier then trying to change to web app mode
JoshBerke
A: 

Hmmm this is the same as what I posted above, I just don't have to cast the type?

I have tried this and get the exact same problem, I cannot access the public properties I have added to my UserControl?? Any more ideas?

leen3o
A: 

You need to declare it as your control type to access it's properties.

public MyMessageBox BCError;
BCError = (MyMessageBox )Master.FindControl("BCError");
Blake
Thanks for replying, but I'm not sure where I would put this? My child page does not understand what the object MyMessageBox is?
leen3o
A: 

try using this in the pages that inherit from your master page:

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

Israel Johnson