Solved

Cant find a label control in the code behind

Posted on 2011-03-23
4
496 Views
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">
                                <table>
                                    <tr>
                                        <td>
                                            <asp:Panel ID="panelSimple" CssClass="float_left" runat="server" GroupingText="User"
                                                EnableViewState="false">
                                                <table>
                                                <tr>
                                                <td>
                                                    User Name: <asp:Label ID = "UserName" runat = "server"></asp:Label>
                                                </td>
</tr>
...
</table>
</Panle>
..
...
</td>
</tr>
</table>
</div>

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

0
Comment
Question by:patd1
[X]
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
4 Comments
 
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;
0
 
LVL 7

Accepted Solution

by:
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.
0
 

Author Comment

by:patd1
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.
0
 
LVL 7

Expert Comment

by:JosephEricDavis
ID: 35693646
Are you going to close this question?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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