views:

78

answers:

4

My master page code looks something like this:

namespace Recipes
{
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        ...

        public void UpdateUserLogin()
        {
            NicknameLbl.Text = ((Recipes.BasePage)Page).CurrentUser.Nickname;
        }

        ...
    }
}

I want to call the UpdateUserLogin() method from a user control, something like this:

((Recipes.MasterPage)this.Page.Master).UpdateUserLogin();

But for some reason the compiler doesn't know Recipes.MasterPage (are you missing an assembly blablabla). Sorry I can't show the exact error message, it's in French.

Maybe the problem is that I added the Recipes namespace around MasterPage manually, it wasn't added by VS. By the way I'm using VS Web Developer Express 2008.

Do you have any idea how I can make this call work?

A: 

Both the MasterPage and the UserControl are child controls of the page they are used by. Your UserControl could potentially be used in a page that doesn't use your MasterPage, and so calling UpdateUserLogin() would not be valid.

You can check it like this, however, and make your call conditionally:

if (Page.Master is MasterPage)
{
    ((MasterPage)Page.Master).UpdateUserLogin();
}

UPDATE It seems you were already aware of that, sorry. Your question is about the reference not working. What is the namespace of your UserControl?

Samuel Meacham
The user control is in the `Recipes` namespace as well. I also added the namespace manually.
Bastien Léonard
A: 

I would recommend data binding the NicknameLbl to the CurrentUser.Nickname property. Then the NicknameLbl text will get updated automatically if the property changes.

Derek Greer
Thanks, I'll give it a try.
Bastien Léonard
A: 
  1. include a MasterType directive at the top of the Content page ASPX file:

    '<%@ MasterType virtualpath="~/DetailsMaster.master" %>'

  2. include a public method in the Master page

    public void UpdateUserLogin(string value) { NicknameLbl.Text = value; }

  3. access the method from the Content page using the Master syntax:

    Master.UpdateUserLogin(Some Text");

http://www.codeproject.com/KB/aspnet/Master_and_Contents.aspx

Space Cracker
It doesn't seem to be valid for a control. But thanks.
Bastien Léonard
I do advice against using MasterType virtualpath, see http://stackoverflow.com/questions/1998931/how-to-fix-namespace-problem-with-autogenerated-master-property-if-mastertype-is
citronas
@Bastien it's valid for control and I already use it and it work very good
Space Cracker
A: 

If your project is a Web Site Project (instead of a Web Application Project), then you do not have a project namespace. All code that is referenced from aspx.cs or master.cs files needs to be stored inside the App_Code directory, as the ASP.Net compiler will create several assemblies instead of just 1, and its not predictable which assembly will contain which aspx code.

Update after 1st comment:

The .ascx.cs and .aspx.cs stay where VS puts them. But it you want to reference classes etc, this needs to be placed inside App_Code, e.g. your Recipes.MasterPage or Recipes.BasePage objects.

devio
Do you mean that .ascx.cs files (user control code files) should be in App_Code?
Bastien Léonard