Solved

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

Posted on 2007-03-21
11
482 Views
Last Modified: 2013-11-07
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.
0
Comment
Question by:jammy-d0dger
  • 6
  • 5
11 Comments
 
LVL 11

Expert Comment

by:ethoths
ID: 18764227
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();
    }
}
0
 

Author Comment

by:jammy-d0dger
ID: 18764338
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 ?
0
 
LVL 11

Accepted Solution

by:
ethoths earned 500 total points
ID: 18764456
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
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:jammy-d0dger
ID: 18764871
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.
0
 
LVL 11

Expert Comment

by:ethoths
ID: 18764934
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.




0
 

Author Comment

by:jammy-d0dger
ID: 18765152
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.

0
 
LVL 11

Expert Comment

by:ethoths
ID: 18765394
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"


0
 

Author Comment

by:jammy-d0dger
ID: 18765611
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...
0
 

Author Comment

by:jammy-d0dger
ID: 18765658
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.
0
 
LVL 11

Expert Comment

by:ethoths
ID: 18765687
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

0
 
LVL 11

Expert Comment

by:ethoths
ID: 18765694
Sorry crossed posts. TRhanks for the feedback.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question