Solved

Cant find a label control in the code behind

Posted on 2011-03-23
4
482 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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