views:

34

answers:

2

I'm embarrassed to ask this here because it's clearly been duplicated several times already on StackOverflow. I've read a lot of stuff including:

http://msdn.microsoft.com/en-us/library/xxwa0ff0

http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

http://www.velocityreviews.com/forums/t110056-cannot-access-strongly-typed-properties-in-master-page.html

I think I've done exactly what those article say, but it's not working for me.

Here's the top of my master page, named "MasterNoNews.master":

<%@ Master Language="C#" %>
<%@ Import Namespace="MyMediHealth.DataAccess" %>
<%@ Import Namespace="MyMediHealth.DataAccess.Containers" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<script runat="server">
    public virtual UserContainer CurrentUser;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
    }
</script>

Here's the top of my child page, named "MyProfile.aspx":

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterNoNews.master"
AutoEventWireup="true" CodeBehind="MyProfile.aspx.cs" 
Inherits="MyMediHealth.Interface.MyProfile" %>
<%@ MasterType VirtualPath="~/MasterNoNews.master" %>

Here's the code-behind on my child page, named "MyProfile.aspx.cs" that's not working:

using System;
using System.Web.UI;
using MyMediHealth.DataAccess.Containers;
using MyMediHealth.DataAccess;

namespace MyMediHealth.Interface
{
    public partial class MyProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // doesn't work
            UserContainer user = Master.CurrentUser;
            // also doesn't work
            UserContainer user = ((MasterNoNews)this.Master).CurrentUser
        }    
    }    
}

In the first case, VS is telling me System.Web.Ui.MasterPage does not contain a definition for CurrentUser. In the second case, VS says the type or namespace 'MasterNoNews' could not be found.

What am I doing wrong?

A: 

This will not work because you have created properties within Master page (and not code-behind). Typically, master page code would get compiled dynamically and a class name (depending on file name) will be created in a temporary assembly. As you are not reffering to this generated class name within temp assembly, you get error. Correct way will be to have code behind file for the master. For example, master will be

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterNoNews.master.cs" Inherits="[Your Namespace].MasterNoNews" %>

And in code-behind file (MasterNoNews.master.cs) have your properties:

namespace [Your Namespace]
{

    public partial class MasterNoNews : System.Web.UI.MasterPage
    {
      public virtual UserContainer CurrentUser;

      protected void Page_Load(object sender, EventArgs e)
      {
        if (Request.IsAuthenticated)
            CurrentUser = new MembershipQueries().getUserFromUserIdName(Page.User.Identity.Name);
      }
    }
}

Now the second syntax would work i.e.

UserContainer user = (([Your Namespace].MasterNoNews)this.Master).CurrentUser
VinayC
A: 

You'll probably still need to move the master page's code to the code behind, but if you put this,

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

on the aspx page you'll be able to access the master page's public methods directly without all that casting. Like so:

UserContainer user = Page.Master.CurrentUser

If you're using the master page's code from loads of child pages then I'd create an interface and have the master page implement it.

Mark Holland