Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:44:24+00:00 2026-05-27T05:44:24+00:00

I am new to ASP.NET and am battling to understand why I cannot get

  • 0

I am new to ASP.NET and am battling to understand why I cannot get the current user name displayed in the text of a MenuItem. I created a control holding a Menu to place on all pages:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MainMenu.ascx.cs" 
Inherits="Test1.Controls.MainMenu" %>
<asp:Menu ID="NavigationMenu" CssClass="menu" runat="server"  
    EnableViewState="False" IncludeStyleBlock="False" Orientation="Horizontal" 
    MaximumDynamicDisplayLevels="4">
    <Items>
        <asp:MenuItem Text="<%=HttpContext.Current.User.Identity.Name%>" />       
    </Items>
</asp:Menu>
<div>
   User is: "<%=HttpContext.Current.User.Identity.Name%>"
</div>

The DIV at the end is to prove that <%=HttpContext.Current.User.Identity.Name%> does indeed give the current user name (it does). However, it just returns blank when used in the MenuItem Text attribute. What is the correct way to do this?


UPDATE:
I see this was downvoted without any useful comment. I have ammended this as the real question has not really been answered: how do we do this declaratively? It is hard to figure out why it works in the DIV but not four lines above in the menu. It would seem that due to the different phases the page is processed in it may be impossible to do this declaratively?

These similar posts suggests that it it impossible:
How do I make my ASP.NET server control take an embedded code block as a property value?
Why will <%= %> expressions as property values on a server-controls lead to a compile errors?

If you want to know why I want to do this it relates to another question I made:
How to specify path relative to current Master Page File

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:44:25+00:00Added an answer on May 27, 2026 at 5:44 am

    The thing you are using does not work for the MenuItem as Steve B found out. I’m still working out why that is..

    The easiest way is to do it in the code behind.

            string username = "Not Logged In";
    
            if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
            {
                username = HttpContext.Current.User.Identity.Name;
            }
    
            NavigationMenu.Items.Add(new MenuItem(username));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create a new asp.net custom control. This control get its data via a
I created a new ASP.NET MVC application with a Project Name of HelloWorld, and
I've created a new ASP.NET Web Site in VS2008. I'm attempting to rebuild an
I created a new ASP.NET MVC application. The home page looks like this: I
I created a new ASP.NET website using Visual Web Developer 2008 Express edition and
In a brand new ASP.NET MVC2 project, I want the user to be redirected
I've created a new ASP.NET website. I've generated an Entity Data Model from my
I created a new ASP .net mvc 2 web application with that default template
I created a new ASP.net MVC Web Application project. Inside my application, I created
I started with a new ASP.NET MVC 2 Web Application template and created a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.