简体   繁体   English

OnClick 事件未触发按钮单击 ASP.NET

[英]OnClick Event Not Firing on Button Click ASP.NET

I have an asp:Button that is in a JavaScript dialog window.我有一个位于 JavaScript 对话框窗口中的 asp:Button。 It has an OnClick event called DialogWindowButton_Click as you can see in the code below.它有一个名为 DialogWindowButton_Click 的 OnClick 事件,如下面的代码所示。 The event is not firing and I have put breakpoints in the C# file and it is not even entering the function.事件没有触发,我在 C# 文件中放置了断点,它甚至没有进入函数。 I'm not sure why and have looked at other forum posts to try to figure this out.我不知道为什么,并查看了其他论坛帖子以试图解决这个问题。 I have 1) deleted the button and recreated the button and OnClick event themselves (this didn't work), and 2) added CausesValidation="False" to the asp:Button tag.我已经 1) 删除了按钮并自己重新创建了按钮和 OnClick 事件(这不起作用),以及 2) 将 CausesValidation="False" 添加到 asp:Button 标记。 Neither avenue has worked.这两条路都没有奏效。 What I have is shown below:我所拥有的如下所示:

<div style="margin:auto; width:100px; padding-bottom:15px;">
    <asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry" 
    OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>

Then in the C# file, I have:然后在 C# 文件中,我有:

    ...
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void DialogWindowButton_Click(object sender, EventArgs e)
    {
        DialogWindowButton_ClickHelper();
        ...
    }

    protected AddressBookEntry DialogWindowButton_ClickHelper()
    {
        ...
    }
    ...

I have the correct file for the CodeBehind tag as well as for the Inherits tag.我有正确的 CodeBehind 标签和 Inherits 标签文件。 In the C# file you can see that the original OnClick event calls on a helper function defined directly below it, but breakpoints in the top of DialogWindowButton_Click() aren't being reached.在 C# 文件中,您可以看到原始 OnClick 事件调用直接在其下方定义的辅助函数,但未到达 DialogWindowButton_Click() 顶部的断点。 There are no build errors either.也没有构建错误。 Could there be something else I'm missing?难道还有什么我想念的吗? Thank you!谢谢!

You need to set UseSubmitBehavior to false (default is true):您需要将UseSubmitBehavior设置为false (默认为 true):

 <asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry" 
    UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />

From Reference :参考

Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism.使用 UseSubmitBehavior 属性指定 Button 控件是使用客户端浏览器的提交机制还是 ASP.NET 回发机制。 By default the value of this property is true, causing the Button control to use the browser's submit mechanism.默认情况下,此属性的值为 true,导致 Button 控件使用浏览器的提交机制。 If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.如果指定 false,则 ASP.NET 页面框架会将客户端脚本添加到页面以将表单发布到服务器。

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

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