Solved

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

Posted on 2007-03-21
11
472 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now