Can't access the User class in a Master Page.

Why can't I access the user class in a master page?

Whenever I try and reference the property in my sitename.master page it just throws an error, "Name User is not declared".

Very frustarting as I need to set the value of another control in the master page based on the username. And I don't want to have to set it on every page that uses the master page.

Help appreciated.
Who is Participating?
ethothsConnect With a Mentor Commented:
This is the master page code behind class. Below is the VB.Net conversion.

Public Partial Class MasterPage
Inherits System.Web.UI.MasterPage
    protected Sub Page_Load(ByVal Sender as object, Byval e as  EventArgs)
        Me.Label1.Text = Me.Context.User.Identity.Name.ToString();
    end sub
end class
This works for me...

public partial class MasterPage : System.Web.UI.MasterPage
    protected void Page_Load(object sender, EventArgs e)
        this.Label1.Text = this.Context.User.Identity.Name.ToString();
jammy-d0dgerAuthor Commented:
sorry, should have stated in question... any chance of conversion to VB please?

Also, where do you put this code, in the Master Page itself ?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

jammy-d0dgerAuthor Commented:

But although this solves the problem with accessing the User class, it now fails to access the other objects on the page, so for instance, in your example, the part Me.Label1.Text would throw the error:

'Label1' is not a member of 'ASP.sitename_master.MasterPage'

(obviously I've tried changing your code to reference an object that does exist on my page!).
maybe something simple got lost in translation but I'm too new to this to suss out what it is.
In my example lable1 is a control on my master page. If you want to access controls on the content pages do so in the content pages events (Page_Load fro example) themselves. If you need to access data calculated in the matsre page to use on the content page, make the items proerties of the master page and access them using the syntax. However, a better way of doing this would be to put the code in a base page and have all your content pages inherit from that page.

jammy-d0dgerAuthor Commented:
No, I want to access a control on the master page itself, from the page load of the master page.

The control in question is a xmlDataSource and I need to set it's DataFile property from the page_load of the master page, using the User.Identity.Name value as part of this DataFile value, eg: in the page_load of the Master Page I want to do this:

xmlDefaultNav.DataFile = "~/users/xmlnav/" & Users.Identity.Name & ".xml"

To clarify, the xmlDefaultNav is on the master page, not on a content page.

Thanks for help so far.

My example was used to set the text of a lable on the master page from the master page on l;oad event. Here is my markup tjat goes with the code snippet above...

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">

I'm not sure why this does not work for you but I notice that your code might not be correct,
    xmlDefaultNav.DataFile = "~/users/xmlnav/" & Users.Identity.Name & ".xml"
should be
    xmlDefaultNav.DataFile = "~/users/xmlnav/" & Me.Context.User.Identity.Name.ToString() & ".xml"

jammy-d0dgerAuthor Commented:
yeah, sorry, I was sloppy typing that example out.  That was how it looked originally, I appreciate that I can now reference Users using the method you describe but I still can't access the xmlDataSource control from the same code!  Driving me nuts...
jammy-d0dgerAuthor Commented:
Cracked it!  I added this to the top of the Master Page:

<%@ Import Namespace="System.Web.UI.MasterPage" %>

And then I can access all controls as before that are on the master page but can now also use User.Identity.Name as required by prefixing with Me.Context as you suggested.

I got there eventually with your help, much appreciated.  I now have a very funky user-specific xml-driven treeview for navigation that changes based on who logs in.

Thanks for your help.
Ok. If the datasource control is on the master page you have 2 options...
1) Try to figure out in the page life cycle where the control gets created (some controsl and in particular databound controls have an odd event life-cycle). then put your code in an event that follows the creation of the datasource control
2) Create the control programaticall in the master pages's on load event (using new). You can be sure that the control exists .

Alternatiley you could create your own datasouce control inherited from the stock one and override it's on load event to get the user (Me.Page.User.Identity.Name.ToString()). I'd probably go this route as it's less combersome

Sorry crossed posts. TRhanks for the feedback.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.