C# - ASP.NET 按钮单击事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1430430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:26:16  来源:igfitidea点击:

C# - ASP.NET Button click event not working

c#asp.netevent-handlingobout

提问by MGOwen

I have an ASP.NET (C#) page with some 3rd party controls, some ajaxy stuff and some normal ASP.NET Button controls.

我有一个 ASP.NET (C#) 页面,其中包含一些 3rd 方控件、一些 ajaxy 内容和一些普通的 ASP.NET Button 控件。

The Button click events do not fire when clicked.

单击时不会触发 Button 单击事件。

Double-clicking the button in design mode in VS 2008 switches to the code-behind but doesn't create the event handler.

在 VS 2008 的设计模式下双击按钮会切换到代码隐藏,但不会创建事件处理程序。

Creating the event handler manually doesn't help.

手动创建事件处理程序无济于事。

The whole page is too big to include here, but this is the top bit:

整个页面太大而无法包含在此处,但这是最重要的部分:

<%@ Page Language="C#" MasterPageFile="~/basewidepage2.master" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CompanyCompliance.aspx.cs" Inherits="CompanyCompliancePage" Title="3DSS" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Assembly="obout_Grid_NET" Namespace="Obout.Grid" TagPrefix="cc2" %>
<%@ Register Src="usercontrols/CalendarEx.ascx" TagName="CalendarEx" TagPrefix="uc2" %>
<%@ MasterType VirtualPath="~/basewidepage2.master" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceholder1" runat="Server">

    <script type="text/javascript">
         // a bunch of function declarations
    </script>

...and my button declaration on the page:

...以及我在页面上的按钮声明:

<asp:Button ID="LicenseCsvButton" runat="server" Text="Export to CSV" OnClick="LicenseCsvButton_Click" />

...and the code-behind:

...和代码隐藏:

protected void LicenseCsvButton_Click(object sender, EventArgs e)
{
    // get data
    CompanyCompliance cc = new CompanyCompliance(Master.theCompany.ID);
    DataTable dt = cc.BusinessLicenses.Tables[0];

    // send to browser as download
    Tools.SendTableAsCsvToBrowser(Response, dt, "LicenseData");
}

Any ideas? could it be the ajax or something? Maybe the 3rd party "obout" grid control?

有任何想法吗?可能是ajax还是什么?也许第 3 方“obout”网格控制?

Update:
I did fix this a month or two ago, so I came back to this question to answer it for posterity but couldn't remember exactly how I fixed it! (Curse you, old age!) I had some limited success by replacing some of the ASP.NET AJAX controls with jQuery UI ones but I think the real solution was that one of the properties in the one of the tags was pointing to a control that no longer existed.

更新:
我在一两个月前确实修复了这个问题,所以我回到这个问题来为后代回答它,但不记得我是如何修复它的!(诅咒你,老了!)通过用 jQuery UI 控件替换一些 ASP.NET AJAX 控件,我取得了一些有限的成功,但我认为真正的解决方案是其中一个标签中的一个属性指向一个控件不再存在。

If you're in this same situation, try that and let me know what works in the comments and I'll post the correct answer.

如果您处于同样的情况,请尝试并让我知道评论中的内容,我会发布正确的答案。

回答by TJB

Change

改变

CodeFile="CompanyCompliance.aspx.cs" 

to

CodeBehind="CompanyCompliance.aspx.cs"

Related question: CodeFile vs CodeBehind

相关问题:CodeFile 与 CodeBehind

回答by 360Airwalk

The inherits directive seems to point to a non-existant type.

继承指令似乎指向一个不存在的类型。

Take a look in the code behind file of the CompanyCompliance page - ("CompanyCompliance.aspx.cs").

查看 CompanyCompliance 页面的代码隐藏文件 -(“CompanyCompliance.aspx.cs”)。

you should find the correct namespace and something like "public partial class xxx" where xxx is the name of the class which should correspond with your inherits directive. maybe you have to fully qualify the name as TJB already stated.

您应该找到正确的命名空间和类似“public partial class xxx”的名称,其中 xxx 是应该与您的继承指令相对应的类的名称。也许您必须完全限定 TJB 已经声明的名称。

回答by annakata

Experienced the same issue recently - cause was eventually determined to be that the button was disabled by a juavascript method onbeforesubmit (designed to prevent multiple submissions), and unfortunately web forms appears to check the enabled state of the button before it allows the codebehind event to trigger.

最近遇到了同样的问题 - 原因最终确定是该按钮被 juavascript 方法 onbeforesubmit(旨在防止多次提交)禁用,不幸的是,Web 表单似乎在允许代码隐藏事件之前检查按钮的启用状态扳机。

回答by Charlie A

During debugging, check the CausesValidation property of your button. For some reason, one of my pages had a button defaulting to "True." When I explicitly set it to "False" everything worked.

在调试期间,检查按钮的 CausesValidation 属性。出于某种原因,我的其中一个页面有一个默认为“True”的按钮。当我明确将其设置为“False”时,一切正常。

回答by tomsv

I had this problem just now and it was caused by another control having an AutoPostBack="true" which would submit the form before the button had a chance to submit the form to trigger the click event.

我刚才遇到了这个问题,它是由另一个具有 AutoPostBack="true" 的控件引起的,它会在按钮有机会提交表单以触发点击事件之前提交表单。

回答by Felipe Castro

I know that this should be resolved by now, but just to share - I just had a similar issue. In my case, the problem was that I had a RequiredFieldValidatorin a page, that wasn't visible because it was part of a popup. That validator was supposed to be invoked only when clicking its related "Save" button, but since I didn't have any ValidationGroupset, it would prevent the page to submit for any clicked button. Adding a ValidationGroupto the right button resolved the issue.

我知道现在应该解决这个问题,但只是分享 - 我刚刚遇到了类似的问题。就我而言,问题是我RequiredFieldValidator在页面中有一个,它不可见,因为它是弹出窗口的一部分。该验证器应该仅在单击其相关的“保存”按钮时调用,但由于我没有任何ValidationGroup设置,它会阻止页面为任何单击的按钮提交。将 a 添加ValidationGroup到右侧按钮解决了该问题。

回答by Brett Weber

For future answer seekers : If you are assigning the click handler in code behind, make sure that it is not inside an IsPostBack == false check :

对于未来的答案寻求者:如果您在后面的代码中分配点击处理程序,请确保它不在 IsPostBack == false 检查中:

void Page_Load (object oSender, EventArgs oEventArgs)
{
   if (IsPostBack == false)
   {
       oButton += new EventHandler(oButton_Click); // does not work
   }
   oButton += new EventHandler(oButton_Click); // does work
}

回答by K S BABJI

I had this problem I have changed the Button property "UseSubmitBehavior"=false. Now it is working.

我有这个问题我已经改变了按钮属性“UseSubmitBehavior”=false。现在它正在工作。

回答by Francisco

I had a similar issue, and it was really tricky. I recently migrated a VS 2005 ASP.NET 2.0 web forms app to VS 2010 ASP.NET 4.0.

我有一个类似的问题,这真的很棘手。我最近将 VS 2005 ASP.NET 2.0 Web 表单应用程序迁移到 VS 2010 ASP.NET 4.0。

It was difficult to setup and buggy as expected, but the issue of buttons not firing the click event was getting on my nerves.

正如预期的那样难以设置和出错,但是按钮不触发点击事件的问题让我很紧张。

In the end, it was as simple as not declaring a submit action in the site.master form. I changed:

最后,就像不在 site.master 表单中声明提交操作一样简单。我变了:

<FORM runat="server" name="MainForm" method="post" action="Default.aspx" id="m_mainForm" enctype="multipart/form-data">

to

<FORM runat="server" name="MainForm" method="post" id="m_mainForm" enctype="multipart/form-data">

Boom. Problem solved. I hope it saves days of troubleshooting to someone. Sadly, I couldn't find anything about it on the internet by myself.

繁荣。问题解决了。我希望它可以为某人节省几天的故障排除时间。可悲的是,我自己在互联网上找不到任何关于它的信息。