C# asp.net Button OnClick 事件未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15569857/
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-10 17:12:51  来源:igfitidea点击:

asp.net Button OnClick event not firing

c#asp.net

提问by Amjad Shah

I have problem in asp.net button control.

我在 asp.net 按钮控件中遇到问题。

I define a button in form, onclickevent of button is not firing when I click on the button.

我在表单中定义了一个按钮,onclick当我点击按钮时,按钮事件不会触发。

<asp:Button ID="btn_QuaSave" runat="server" Text="SAVE" OnClick="btn_QuaSave_Click" /> 

protected void btn_QuaSave_Click(object sender, EventArgs e)
{

}

采纳答案by Lingaraj

Have you copied this method from other page/application ? if yes then it will not work, So you need to delete the event and event name assigned to the button then go to design and go to button even properties go to onClick event double click next to it, it will generate event and it automatically assigns event name to the button. this should work

您是否从其他页面/应用程序复制了此方法?如果是,那么它将不起作用,因此您需要删除分配给按钮的事件和事件名称,然后转到设计并转到按钮甚至属性转到onClick事件旁边双击它,它将生成事件并自动分配按钮的事件名称。这应该有效

回答by Freelancer

Try to Cleanyour solution and then try once again.

尝试Clean您的解决方案,然后再试一次。

It will definitely work. Because every thing in code seems to be ok.

它肯定会起作用。因为代码中的每一件事似乎都没问题。

Go through this link for cleaning solution>

通过此链接获取清洁解决方案>

http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/e53aab69-75b9-434a-bde3-74ca0865c165/

http://social.msdn.microsoft.com/Forums/en-US/vsdebug/thread/e53aab69-75b9-434a-bde3-74ca0865c165/

回答by Paritosh

Try to go into Design mode in Visual Studio, locate the button and double click the button that should setup the event. Otherwise once the button is selected in Design more, go to the properties and try setting it from there.

尝试在 Visual Studio 中进入设计模式,找到按钮并双击应该设置事件的按钮。否则,一旦在 Design more 中选择了按钮,请转到属性并尝试从那里进行设置。

回答by Selmanist

If it's throwing no error but still not firing the click event when you click the submit button, try to add action="YourPage.aspx"to your form.

如果在您单击提交按钮时它没有抛出错误但仍然没有触发 click 事件,请尝试添加action="YourPage.aspx"到您的表单中。

回答by Tuyen Nguyen

In my case I put required="required"inside CKEditorcontrol.
Removing this attribute fixed the issue.
Before

就我而言,我将required="required"放在CKEditor控件中。
删除此属性修复了问题。

<CKEditor:CKEditorControl ID="txtDescription" BasePath="/ckeditor/" runat="server" required="required"></CKEditor:CKEditorControl>

After

<CKEditor:CKEditorControl ID="txtDescription" BasePath="/ckeditor/" runat="server"></CKEditor:CKEditorControl>

回答by Ravan

If you are using updatepanel on onclick event, this may happen.

如果您在 onclick 事件上使用 updatepanel,则可能会发生这种情况。

Use 'EnableEventValidation="false"' in your page markup like this :

在您的页面标记中使用“EnableEventValidation="false"”,如下所示:

<%@ Page Language="C#" MasterPageFile="~/ars_home.master" AutoEventWireup="true" CodeFile="Transaction_Window.aspx.cs" Inherits="Transaction_Window" EnableEventValidation="false" %>

Hope this helps

希望这可以帮助

回答by Ravan

Add validation groups for your validator elements. This allows you distinguish between different groups which to include in validation. Add validation group also to your submit button

为验证器元素添加验证组。这允许您区分要包含在验证中的不同组。将验证组也添加到您的提交按钮

回答by Chani Poz

Because your button is in controlit could be that there is a validation from another control that don't allow the button to submit. The result in my case was to add CausesValidationproperty to the button:

因为您的按钮在control其中可能是来自另一个控件的验证不允许该按钮提交。在我的情况下,结果是向CausesValidation按钮添加属性:

<asp:Button ID="btn_QuaSave" runat="server" Text="SAVE" OnClick="btn_QuaSave_Click" CausesValidation="False"/> 

回答by Mohsen.Sharify

in my case: make sure not exist any form element in your page other than top main form, this cause events not fired

就我而言:确保页面中除了顶部主表单之外不存在任何表单元素,这会导致事件未触发

回答by waqar haider

i had the same problem did all changed the button and all above mentioned methods then I did a simple thing I was using two forms on a single page and form with in the form so I removed one and it worked :)

我遇到了同样的问题,所有的按钮和上面提到的方法都改变了,然后我做了一件简单的事情,我在一个页面上使用了两个表单,并在表单中使用了表单,所以我删除了一个,它起作用了:)