1

I have a button in my web site that doesn't work at all. Every time I write my script inside the button event in code behind I get this error:

 Server Error in '/website.com' Application.
  Compilation Error
   Description: An error occurred during the compilation of a resource       
   required to service this request. Please review the following specific 
   error details and modify your source code appropriately.

   Compiler Error Message: CS1061: 'ASP.test_aspx' does not contain a 
   definition for 'btnSubmit_Click' and no extension method 
   'btnSubmit_Click' accepting a first argument of type 'ASP.test_aspx' 
   could be found (are you missing a using directive or an assembly 
   reference?)

 Source Error:


  Line 5:  
  Line 6:  
  Line 7:  <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
     OnClick="btnSubmit_Click" />
  Line 8:  
  Line 9:  

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace website
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblmsg.Text = "Hello World!";
        }
    }
}

Source :

  <%@ Page Title="" Language="C#" MasterPageFile="~/webMaster.Master"  
  AutoEventWireup="True" CodeBehind="Test.aspx.cs"  
  Inherits="website.Test"  %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
     runat="server">
      <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
       OnClick="btnSubmit_Click" CausesValidation="False" />

      <asp:Label ID="lblmsg" runat="server"></asp:Label>  
    </asp:Content>

This is my Test.aspx.designer.cs code:

 namespace website {
     public partial class Test {

    /// <summary>
    /// btnSubmit 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.Button btnSubmit;

    /// <summary>
    /// lblmsg 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 lblmsg;
   }
 }

Update:

I solved the problem after changing CodeBehind="Test.aspx.cs" to CodeFile="Test.aspx.cs". More information CodeFile vs CodeBehind

Community
  • 1
  • 1
Max
  • 19
  • 3
  • 9

2 Answers2

1

You did not show the 3rd part of required code, where the event gets initialized with the delegate, that kind of stuff. Automatically created code, that we normally don't pay much attention to. See method InitializeComponent() in file Formxxx.Designer.cs.

To keep it simple, I would suggest: just remove the button click event handler and create it again with Visual Studio by clicking the button in the designer. Then all required code parts get created correctly.

Roland
  • 4,619
  • 7
  • 49
  • 81
  • I have removed the old button and then double clicked from the design panel and created event handler associated with the new added button and pasted my code in there! I also ran rebuild my solution and removed the old test.aspx.designer.cs and right clicked on the test.aspx to convert to application to make new designer. All of these DID NOT work for me! – Max Feb 26 '15 at 09:32
  • You don't have to paste anything. Just create the button and the event in Visual Studio and set a debugger breakpoint in the empty event handler. Check that the event handler is executed when the button is clicked. If that is working, then start putting in your code in the event handler. But if it is NOT working, you have to fix your solution and/or project that may have become corrupt. – Roland Feb 27 '15 at 10:05
  • I replaced the old button with a new one, left breaking point on the new button (with no codes in). The button worked in my development machine. However, it didn't work when deployed in the hosting website and it gave a Compilation Error! – Max Feb 27 '15 at 12:59
  • @Max Looks like a deployment problem. Same CPU architecture (32/64bit) ? Do you have all debug options enabled on the hosting website? Different IIS settings on development and hosting web sites? What is the exact error msg? At least I guess the error msg is different from your original posting above, because now the `btnSubmit_Click` should be present. Did you do a `publish` in VS? – Roland Feb 27 '15 at 13:39
  • Just quick update. I have just changed the codebehind to CodeFile and the button worked! – Max Feb 27 '15 at 13:45
  • @Max Great! Perhaps good to mention this in your question description, and put the word `Solved` in the title. Have a nice weekend – Roland Feb 27 '15 at 14:31
0

Just remove the click event protected void btnSubmit_Click(object sender, EventArgs e)from backend and OnClick="btnSubmit_Click" from the aspx build the solution and then again define the click event and bind it.

This happens when you delete the auto generated click event.

Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47