Solved

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

Posted on 2007-03-21
11
479 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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