views:

192

answers:

3

I have the following button on a .aspx page:

<asp:Button runat="server" ID="bntLogin" OnClick="bntLogin_Click" Text="Login" />

With the following in the .aspx.cs:

protected void bntLogin_Click(object sender, EventArgs e)
        {
           //
        }

When I try to build it I get the following error:

'ASP.reserve_aspx' does not contain a definition for 'bntLogin_Click' and no extension method 'bntLogin_Click' accepting a first argument of type 'ASP.reserve_aspx' could be found (are you missing a using directive or an assembly reference?)

However, when I move the click event from the code-behind to a script block inside the markup it builds.

Any ideas?

+5  A: 

Are you sure that you placed the method in the correct code-behind file?

Check which file you are using as your code-behind file by looking at the @page directive at the top of the aspx file itself (check the inherits attribute) - you may be surprised.


A loosely-related side note: By setting the OnClick with a string value that corresponds to the method you wish to invoke you are implicitly relying on ASP.NET's AutoEventWireup feature which I don't consider to be a good approach. It is better to manually wire up your controls in your page's OnInit override method like this:

bntLogin.Click += bntLogin_Click;

By relying on AutoEventWireup you are allowing the ASP.NET runtime to create this code for you and since this happens at execution time you are incurring an execution time performance penalty as well as risking an exception like the one you are seeing now.

Andrew Hare
Yea, I double checked, its in the correct code-behind and inheriting the correct class.
Neil
Okay, try removing the `OnClick` attribute from your aspx file and adding the code I have posted above.
Andrew Hare
My understanding is that if you use AutoEventWireup, then it is unnecessary to explicitly declare event handlers like OnClick in the aspx.
recursive
@recursive - That is correct.
Andrew Hare
A: 

I usually hook up the events in the code-behind's OnInit method, which is a hold-over from ASP.NET 1.1 (that's where the designer would put it back then.)

Broam
A: 

If by any chance you're still running ASP.NET 1.1, delete the aspnet_client folder, rebuild and try again.

IrishChieftain