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?

NoWar
  • 36,338
  • 80
  • 323
  • 498

13 Answers13

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.

Matthew Dresser
  • 11,273
  • 11
  • 76
  • 120
  • 19
    To clarify: Delete the .designer.cs file then right click on the .aspx file and select "Convert to Web Application". – Nathan Taylor Aug 22 '09 at 19:30
  • 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. –  Aug 22 '09 at 19:49
  • 1
    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. – Jono Feb 12 '13 at 03:12
  • If your designer file doesn't even exist, try the second paragraph in the answer at https://stackoverflow.com/a/45327/2615878. – Mike Grove aka Theophilus Oct 23 '18 at 13:48
11

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";
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
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.

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 3
    I'd always heard we shouldn't manually modify the designer file because changes you make could be overridden by the IDE. – joshmcode Jun 11 '15 at 15:07
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" %>
Kohen Holms
  • 143
  • 1
  • 6
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

Lightning3
  • 375
  • 7
  • 18
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.
alexyorke
  • 4,224
  • 3
  • 34
  • 55
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

Dr TJ
  • 3,241
  • 2
  • 35
  • 51
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.

Darthg8r
  • 12,377
  • 15
  • 63
  • 100
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.

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Reginaldo Rigo
  • 297
  • 3
  • 13
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.

Sandip.Nascar
  • 335
  • 1
  • 7
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 ;)

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!

Protector one
  • 6,926
  • 5
  • 62
  • 86
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

netfed
  • 602
  • 8
  • 18