Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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";

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" %>

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.

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

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

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.

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.

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!

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.

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.

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

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 ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top