Link to home
Start Free TrialLog in
Avatar of jammy-d0dger
jammy-d0dger

asked on

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 user.identity.name 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.
Avatar of ethoths
ethoths

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();
    }
}
Avatar of jammy-d0dger

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of ethoths
ethoths

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks,

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 me.page.master.property 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.




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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

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"


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...
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
or
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.