Cant find a label control in the code behind

Posted on 2011-03-23
Last Modified: 2012-05-11
I have default.aspx page and default.aspx.cs page. I just added a label (UserName) to my default.aspx page and tried to set the text of that label in the page_load method in the code behind. I get a compilation error: UserName does not exist in the current context. This seems to be the simplest change but is not working. Please check my code and advise change.

Thank You.

<%@ Page Language="C#" MasterPageFile="MyMaster.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" EnableViewState="true" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit"%>

<div id="simple">
                                            <asp:Panel ID="panelSimple" CssClass="float_left" runat="server" GroupingText="User"
                                                    User Name: <asp:Label ID = "UserName" runat = "server"></asp:Label>

Open in new window

public partial class _Default : MyUserInformation
protected void Page_Load(object sender, EventArgs e)
        string UserRole = this.MuUserRole;
        UserName.Text = this.MyUserRole;
        //this.UserName.Text = this.MyUserRole;




Open in new window

Question by:patd1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 35201449
Make sure your Default.aspx.designer.cs file has something like this in it:

        /// <summary>
        /// UserName control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.WebControls.Label UserName;

Accepted Solution

JosephEricDavis earned 500 total points
ID: 35201465
Sometimes this happens when visual studio fails to update the designer file associated with the aspx page and the code behind with the information about your control when you make the change.  The designer file contains information that tells the code behind file what is available on the aspx page.  I've have often fixed this problem in the past by doing the following...

1) Delete the associated designer file.  In your case it would be 'Default.aspx.designer.vb'.
2) Right Click on Default.aspx and choose 'Convert to web application'.
3) You will be prompted with a message that tells you that it will create a designer file and a code behind file.  You will click yes.

Don't worry about the code behind file being overwritten.  That won't happen.  It will see that you already have one and leave it alone.  the designer file will be recreated and you should be able to reference your control.

Why does this happen?  Just a little glitch in visual studio.  But this is a known fix.

Author Comment

ID: 35201483
Thank for your reply. I just noticed that there were two defaults in solution. I deleted one and it works now.

Thank You.

Expert Comment

ID: 35693646
Are you going to close this question?

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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