C# - ASP.NET 按钮单击事件不起作用
我有一个 ASP.NET (C#) 页面,其中包含一些第 3 方控件、一些 ajaxy 内容和一些普通的 ASP.NET 按钮控件。
单击时不会触发按钮单击事件。
在 VS 2008 的设计模式下双击按钮会切换到代码隐藏,但不会创建事件处理程序。
手动创建事件处理程序没有帮助。
整个页面太大,无法包含在此处,但这是最上面的部分:
<%@ 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>
...以及页面上的按钮声明:
<asp:Button ID="LicenseCsvButton" runat="server" Text="Export to CSV" OnClick="LicenseCsvButton_Click" />
...以及隐藏代码:
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");
}
有什么想法吗?可能是ajax之类的吗?也许是第三方“obout”网格控制?
更新:
我确实在一两个月前解决了这个问题,所以我回到这个问题来为后代回答它,但不记得我到底是如何解决它的! (诅咒你,老了!)通过用 jQuery UI 控件替换一些 ASP.NET AJAX 控件,我取得了一些有限的成功,但我认为真正的解决方案是其中一个标签中的属性之一指向一个控件不再存在了。
如果您遇到同样的情况,请尝试一下,并在评论中告诉我什么有效,我将发布正确的答案。
I have an ASP.NET (C#) page with some 3rd party controls, some ajaxy stuff and some normal ASP.NET Button controls.
The Button click events do not fire when clicked.
Double-clicking the button in design mode in VS 2008 switches to the code-behind but doesn't create the event handler.
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?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在调试期间,检查按钮的 CausesValidation 属性。由于某种原因,我的其中一个页面有一个默认为“True”的按钮。当我明确将其设置为“False”时,一切正常。
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.
我知道这个问题现在应该已经解决了,但只是为了分享 - 我刚刚遇到了类似的问题。就我而言,问题是我在页面中有一个
RequiredFieldValidator
,但它不可见,因为它是弹出窗口的一部分。该验证器应该仅在单击其相关的“保存”按钮时调用,但由于我没有设置任何 ValidationGroup ,因此它将阻止页面提交任何单击的按钮。将ValidationGroup
添加到右侧按钮解决了该问题。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
RequiredFieldValidator
in 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 anyValidationGroup
set, it would prevent the page to submit for any clicked button. Adding aValidationGroup
to the right button resolved the issue.最近遇到了同样的问题 - 最终确定原因是该按钮被 juavascript 方法 onbeforesubmit 禁用(旨在防止多次提交),不幸的是,Web 表单似乎在允许代码隐藏事件之前检查按钮的启用状态扳机。
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.
我刚才遇到了这个问题,它是由另一个具有 AutoPostBack="true" 的控件引起的,该控件会在按钮有机会提交表单以触发单击事件之前提交表单。
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.
更改
为
相关问题:CodeFile 与 CodeBehind
Change
to
Related question: CodeFile vs CodeBehind
继承指令似乎指向不存在的类型。
查看 CompanyCompliance 页面的代码隐藏文件 - (“CompanyCompliance.aspx.cs”)。
您应该找到正确的命名空间和类似“publicpartialclassxxx”的内容,其中xxx是应该与您的inherited指令相对应的类的名称。也许您必须按照 TJB 的规定完全限定该名称。
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").
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.
我也遇到了类似的问题,而且确实很棘手。我最近将 VS 2005 ASP.NET 2.0 Web 表单应用程序迁移到 VS 2010 ASP.NET 4.0。
正如预期的那样,设置起来很困难,而且有很多错误,但是按钮不触发点击事件的问题让我很紧张。
最后,这就像不在 site.master 表单中声明提交操作一样简单。我改变了:
到
Boom。问题解决了。我希望它可以帮助某人节省几天的故障排除时间。遗憾的是,我自己在互联网上找不到任何相关信息。
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.
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:
to
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.
我遇到了这个问题,我更改了按钮属性“UseSubmitBehavior”= false。现在它正在发挥作用。
I had this problem I have changed the Button property "UseSubmitBehavior"=false. Now it is working.
对于未来的答案寻求者:如果您在代码后面分配点击处理程序,请确保它不在 IsPostBack == false 检查内:
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 :