单击按钮时未触发 OnClick 事件 ASP.NET
OnClick Event Not Firing on Button Click ASP.NET
我在 JavaScript 对话框 window 中有一个 asp:Button。它有一个名为 DialogWindowButton_Click 的 OnClick 事件,您可以在下面的代码中看到。该事件没有触发,我在 C# 文件中放置了断点,它甚至没有进入函数。我不确定为什么,并且查看了其他论坛帖子以尝试解决这个问题。我有 1) 删除了按钮并自己重新创建了按钮和 OnClick 事件(这没有用),以及 2) 将 CausesValidation="False" 添加到 asp:Button 标记。这两种途径都没有奏效。我所拥有的如下所示:
<div style="margin:auto; width:100px; padding-bottom:15px;">
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>
然后在 C# 文件中,我有:
...
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DialogWindowButton_Click(object sender, EventArgs e)
{
DialogWindowButton_ClickHelper();
...
}
protected AddressBookEntry DialogWindowButton_ClickHelper()
{
...
}
...
我有 CodeBehind 标签和 Inherits 标签的正确文件。在 C# 文件中,您可以看到原始 OnClick 事件调用直接在其下方定义的辅助函数,但未到达 DialogWindowButton_Click() 顶部的断点。也没有构建错误。我还缺少其他东西吗?谢谢!
您需要将UseSubmitBehavior
设置为false
(默认为真):
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />
来自Reference:
Use the UseSubmitBehavior property to specify whether a Button control
uses the client browser's submit mechanism or the ASP.NET postback
mechanism. By default the value of this property is true, causing the
Button control to use the browser's submit mechanism. If you specify
false, the ASP.NET page framework adds client-side script to the page
to post the form to the server.
我在 JavaScript 对话框 window 中有一个 asp:Button。它有一个名为 DialogWindowButton_Click 的 OnClick 事件,您可以在下面的代码中看到。该事件没有触发,我在 C# 文件中放置了断点,它甚至没有进入函数。我不确定为什么,并且查看了其他论坛帖子以尝试解决这个问题。我有 1) 删除了按钮并自己重新创建了按钮和 OnClick 事件(这没有用),以及 2) 将 CausesValidation="False" 添加到 asp:Button 标记。这两种途径都没有奏效。我所拥有的如下所示:
<div style="margin:auto; width:100px; padding-bottom:15px;">
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>
然后在 C# 文件中,我有:
...
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DialogWindowButton_Click(object sender, EventArgs e)
{
DialogWindowButton_ClickHelper();
...
}
protected AddressBookEntry DialogWindowButton_ClickHelper()
{
...
}
...
我有 CodeBehind 标签和 Inherits 标签的正确文件。在 C# 文件中,您可以看到原始 OnClick 事件调用直接在其下方定义的辅助函数,但未到达 DialogWindowButton_Click() 顶部的断点。也没有构建错误。我还缺少其他东西吗?谢谢!
您需要将UseSubmitBehavior
设置为false
(默认为真):
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />
来自Reference:
Use the UseSubmitBehavior property to specify whether a Button control uses the client browser's submit mechanism or the ASP.NET postback mechanism. By default the value of this property is true, causing the Button control to use the browser's submit mechanism. If you specify false, the ASP.NET page framework adds client-side script to the page to post the form to the server.