Solved

Cant find a label control in the code behind

Posted on 2011-03-23
4
476 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 19

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

13 Experts available now in Live!

Get 1:1 Help Now