title: Button click event is not working in code behind ASP.NET C#
body: <p>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:</p>
  
  <pre><code> 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:  &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" 
       OnClick="btnSubmit_Click" /&gt;
    Line 8:  
    Line 9:  
  </code></pre>
  
  <p>Code Behind</p>
  
  <pre><code>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!";
          }
      }
  }
  </code></pre>
  
  <p>Source :</p>
  
  <pre><code>  &lt;%@ Page Title="" Language="C#" MasterPageFile="~/webMaster.Master"  
    AutoEventWireup="True" CodeBehind="Test.aspx.cs"  
    Inherits="website.Test"  %&gt;
  
      &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt;
      &lt;/asp:Content&gt;
      &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
       runat="server"&gt;
        &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" 
         OnClick="btnSubmit_Click" CausesValidation="False" /&gt;
  
        &lt;asp:Label ID="lblmsg" runat="server"&gt;&lt;/asp:Label&gt;  
      &lt;/asp:Content&gt;
  </code></pre>
  
  <p>This is my Test.aspx.designer.cs code:</p>
  
  <pre><code> namespace website {
       public partial class Test {
  
      /// &lt;summary&gt;
      /// btnSubmit control.
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;
      /// Auto-generated field.
      /// To modify move field declaration from designer file to code-behind  
          file.
      /// &lt;/remarks&gt;
         protected global::System.Web.UI.WebControls.Button btnSubmit;
  
      /// &lt;summary&gt;
      /// lblmsg control.
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;
      /// Auto-generated field.
      /// To modify move field declaration from designer file to code-behind 
          file.
      /// &lt;/remarks&gt;
      protected global::System.Web.UI.WebControls.Label lblmsg;
     }
   }
  </code></pre>
  
  <p>Update:</p>
  
  <p>I solved the problem after changing CodeBehind="Test.aspx.cs" to CodeFile="Test.aspx.cs". More information <a href="https://stackoverflow.com/questions/73022/codefile-vs-codebehind">CodeFile vs CodeBehind</a></p>