简体   繁体   English

asp.net 按钮 OnClick 事件未触发

[英]asp.net Button OnClick event not firing

I have problem in asp.net button control.我在 asp.net 按钮控制中有问题。

I define a button in form, onclick event 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)
{

}

Because your button is in control it could be that there is a validation from another control that don't allow the button to submit.因为您的按钮处于control所以可能是另一个控件进行了验证,不允许按钮提交。 The result in my case was to add CausesValidation property to the button:在我的情况下,结果是将CausesValidation属性添加到按钮:

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

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.如果是,那么它将不起作用,因此您需要删除分配给按钮的事件和事件名称,然后转到设计并转到按钮甚至属性转到onClick事件旁边双击它,它将生成事件并自动分配按钮的事件名称。 this should work这应该有效

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希望这可以帮助

I had the same problem, my aspnet button's click was not firing.我遇到了同样的问题,我的 aspnet 按钮的点击没有触发。 It turns out that some where on other part of the page has an input with html "required" attribute on.事实证明,页面其他部分的某些地方有一个带有 html“必需”属性的输入。

This might be sound strange, but once I remove the required attribute, the button just works normally.这可能听起来很奇怪,但是一旦我删除了 required 属性,按钮就会正常工作。

In my case I put required="required" inside CKEditor control.就我而言,我将required="required"放在CKEditor控件中。
Removing this attribute fixed the issue.删除此属性修复了问题。
Before

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

After

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

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

I had a similar issue and none of the answers worked for me.我有一个类似的问题,没有一个答案对我有用。 Maybe someone finds my solution helpful.也许有人发现我的解决方案有帮助。 In case you do not mind submitting on button click, only attaching to click event setting UseSubmitBehavior="false" may be worth trying.如果您不介意在按钮单击时提交,则仅附加到单击事件设置UseSubmitBehavior="false"可能值得尝试。

Try to Clean your 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/

Try to go into Design mode in Visual Studio, locate the button and double click the button that should setup the event.尝试在 Visual Studio 中进入设计模式,找到按钮并双击应该设置事件的按钮。 Otherwise once the button is selected in Design more, go to the properties and try setting it from there.否则,一旦在 Design more 中选择了按钮,请转到属性并尝试从那里进行设置。

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将验证组也添加到您的提交按钮

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

If the asp button is inside <a href="#"> </a> tag then also the Click event will not raise.如果 asp 按钮在<a href="#"> </a>标签内,则Click事件也不会引发。

Hope it's useful to some one.希望它对某人有用。

In the case of nesting the LinkButton within a Repeater you must using something similar to the following:中继器中嵌套 LinkBut​​ton 的情况下,您必须使用类似于以下内容的内容:

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="MyUpdate">LinkButton</asp:LinkButton>

 protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
 {
    if (e.CommandName.Equals("MyUpdate"))
    {
        // some code
    }
 }

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

Even i had two forms one for desktop view and other for mobile view , Removed one formed worked for me .即使我有两种形式,一种用于桌面视图,另一种用于移动视图,删除了一种对我有用的形式。 I dint knew asp.page should have only one form.我知道 asp.page 应该只有一种形式。

Try this onserverclick试试这个 onserverclick

<button id ="DemoButton" runat="server" onserverclick="button_Click">Login</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM