【问题标题】:Button click event is not working in code behind ASP.NET C#按钮单击事件在 ASP.NET C# 后面的代码中不起作用
【发布时间】:2015-02-25 20:57:02
【问题描述】:

我的网站上有一个按钮根本不起作用。每次我在后面的代码中的按钮事件中编写脚本时,都会收到此错误:

 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:  

代码背后

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

来源:

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

这是我的 Test.aspx.designer.cs 代码:

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

更新:

我将 CodeBehind="Test.aspx.cs" 更改为 CodeFile="Test.aspx.cs" 后解决了问题。更多信息CodeFile vs CodeBehind

【问题讨论】:

  • 似乎很有说服力:ASP.test_aspxwebsite(命名空间)。
  • 什么意思?请解释你的评论。

标签: c# asp.net


【解决方案1】:

您没有显示所需代码的第三部分,即使用委托初始化事件的地方,诸如此类的东西。自动创建的代码,我们通常不太关注。请参阅文件Formxxx.Designer.cs 中的方法InitializeComponent()

为了简单起见,我建议:只需删除按钮单击事件处理程序,然后通过单击设计器中的按钮使用 Visual Studio 再次创建它。然后正确创建所有必需的代码部分。

【讨论】:

  • 我删除了旧按钮,然后从设计面板双击并创建了与新添加的按钮关联的事件处理程序并将我的代码粘贴到那里!我还运行了重建我的解决方案并删除了旧的 test.aspx.designer.cs 并右键单击 test.aspx 以转换为应用程序以创建新的设计器。所有这些都不适合我!
  • 您不必粘贴任何内容。只需在 Visual Studio 中创建按钮和事件,并在空事件处理程序中设置调试器断点。检查单击按钮时是否执行了事件处理程序。如果这有效,则开始将您的代码放入事件处理程序中。但如果它不起作用,您必须修复可能已损坏的解决方案和/或项目。
  • 我用新按钮替换了旧按钮,在新按钮上留下断点(没有代码)。该按钮在我的开发机器上工作。但是,它在托管网站中部署时不起作用,并且出现编译错误!
  • @Max 看起来像一个部署问题。相同的 CPU 架构(32/64 位)?您是否在托管网站上启用了所有调试选项?开发和托管网站上的不同 IIS 设置?确切的错误消息是什么?至少我猜错误消息与您上面的原始帖子不同,因为现在应该存在btnSubmit_Click。你在 VS 中做了publish 吗?
  • 只是快速更新。我刚刚将代码隐藏更改为 CodeFile 并且按钮起作用了!
【解决方案2】:

只需从后端删除点击事件protected void btnSubmit_Click(object sender, EventArgs e),并从aspx构建解决方案中删除OnClick="btnSubmit_Click",然后再次定义点击事件并绑定它。

当您删除自动生成的点击事件时会发生这种情况。

【讨论】:

  • 随你喜欢..你可以用这两种方式中的任何一种来做!
  • 我按照你说的手动试了一次,另一次自动试了,但是没有用!
猜你喜欢
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多