Can't access control ID in code behind
Asked Answered
T

13

25

I have the following code in my aspx page:

 <asp:Button runat="server" ID="myButton" Text="hello" />

and this in my code behind:

protected void Page_Load(object sender, EventArgs e)
{ 
    myButton.Text = "bye"; 
}

For some reason the intellisense picks up the "myButton" id in the code behind but when it compiles it says

it can't build because it doesn't recognise it!

My page is a default aspx page which uses a master page, the button is inside a content control and all is set to run at server correctly, the page runs and displays fine except for this button resolution issue!

Any ideas?

Trevethick answered 22/8, 2009 at 19:6 Comment(1)
Can you paste more of the markup code so we can see the context of the Button?Rapparee
H
32

Sounds like your designer file (PageName.designer.cs) has got a bit messed up. I'd try deleting the button from your page and adding it again.

Honshu answered 22/8, 2009 at 19:28 Comment(4)
To clarify: Delete the .designer.cs file then right click on the .aspx file and select "Convert to Web Application".Rapparee
thanks it appeared as if the default page i was working on didnt even have a designer! i deleted it all and recreated it from scratch.Trevethick
I've found this occurs when you're running a debug server while saving the changes to the .aspx, so it cant modify the designer file. I just add then remove a space from the .aspx after closing debug to fix it.Sporting
If your designer file doesn't even exist, try the second paragraph in the answer at https://mcmap.net/q/80710/-how-do-you-force-visual-studio-to-regenerate-the-designer-files-for-aspx-ascx-files.Calfee
T
12

If the button is inside a naming container like a repeater you won't be able to use it like that. Instead, you need to do something like this:

Button myButton = (Button)Container.FindControl("myButton");
myButton.Text = "bye";
Theorize answered 22/8, 2009 at 19:39 Comment(0)
D
3

I've encountered the same problem, first I was trying to figure what was wrong with my designer.cs file. But after I realized that for my WebForm markup I was using a copy/paste from the Default webform. So the problem was, like Protector one said be sure you check the inherit property of @Page:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="InputValidation._Default" %>
Droit answered 19/5, 2011 at 18:1 Comment(0)
E
3

I found a simple solution for this case.

Just open the designer file and add a line that corresponds to your UI control.

protected global::System.Web.UI.WebControls.Button myButton;

Where myButton is Id of your control.

Encephalo answered 2/4, 2015 at 18:14 Comment(1)
I'd always heard we shouldn't manually modify the designer file because changes you make could be overridden by the IDE.Karimakarin
I
2

I faced the problem and i noticed that Designer.cs files were excluded from my project.
try to look for them include to your project again.
good luck

Inflexible answered 12/3, 2011 at 0:1 Comment(0)
F
2

i know that problem is solved, but when i had a similiar problem, when i have convert WebAplication From c# to VB - i solved this by adding Namespace X / End Namespace

Finke answered 25/9, 2013 at 9:59 Comment(0)
A
2

I had the same problem today and in my case the solution was:

  • Go to: C:\Users\lenovo\AppData\Local\Temp\Temporary ASP.NET Files\
  • Delete the Temporary ASP.NET Files folder.
Aalesund answered 19/8, 2017 at 21:23 Comment(0)
K
1

I've run into this issue a few times. Sometimes it classnames in the designer and code behind don't match. Other times it's the namespace not matching. Other times, the designer page doesn't get the member variable generated.

As mdresser mentioned, regenerating the designer file very well may take care of the problem. Just make sure that you save the aspx page first.

Kerrikerrie answered 22/8, 2009 at 19:39 Comment(0)
A
1

Make sure you have no files that accidentally try to inherit or define the same (partial) class. These files can be completely unrelated to where the error actually appeared!

Autoharp answered 27/4, 2011 at 8:16 Comment(0)
C
1

It's been a long time. Buuuut I was facing this same problem today, so it seems never it's too late. For me it was happening cause the TextBox was inside a nonnamed form. Then I set an id to the form and I could acess the TextBox like this: idForm.idTextBox.ToString(). I guess it is gonna happen the same with a button.

Cognition answered 23/12, 2015 at 0:14 Comment(0)
S
1

Problem is you might have multiple aspx files with codefile in page directive points to same codebehind file. It expects the same control to exists in all the aspx file linked to same code behind and thus throwing compilation error.

Squire answered 27/5, 2016 at 13:53 Comment(0)
O
1

Just change the inherit property under page tag

    <% Page inherit = "Project_Name.otherform" 

change it to

    <% Page inherit = "Project_Name.YourForm"

And you will see in form2.aspx.desginer.cs the class name is also changed and corrected.

Let Me Explain: Let's Say you have 2 web forms test1.aspx and test2.aspx. You copied some piece of code from test1.aspx to test2.aspx, your test2.aspx file will use <% page tag as:

    <% Page inherit = "Project_Name.test1"

which was supposed to be as:

    <% Page inherit = "Project_Name.test2"

because of which your test2.aspx.designer.cs class name will be automatically changed to same as test1.aspx.designer.cs as it is a system generated file and you will be unable to use your control IDs.

After correcting your inherit property, save and Open test2.aspx.designer.cs to verify and you will see in that the class name is also changed. it will be like:

    public partial class test1
    {
     ...

Changed in to

    public partial class test2
    {
    ... 

Thanks me later ;)

Offspring answered 22/4, 2018 at 6:23 Comment(0)
R
0

Check the namespace in all three files. Sometimes there is a discrepancy between the namespace name in the designer.cs file and the other two files. Be sure to check that name space name match in all three files

Russon answered 12/11, 2017 at 4:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.