[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cant find a label control in the code behind

Posted on 2011-03-23
4
Medium Priority
?
506 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

656 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