取消ASP.NET中的默认提交按钮

发布于 2024-08-05 22:25:01 字数 270 浏览 4 评论 0原文

我有一个 ASP.NET 应用程序,其中有一些 ASP.NET 按钮和几个纯 HTML 按钮。只要用户在文本框中按 Enter 键,ASP.NET 按钮就会尝试提交表单。

我知道我可以更改 defaultButton,但我不希望有任何默认按钮。我只是想要它,所以当用户按下 Enter 键时它不会执行任何操作。

我尝试将 defaultButton 设置为空白,但这似乎不起作用。如何防止按下 Enter 键时 ASP.NET 按钮提交表单?

I have an ASP.NET application where there's a few ASP.NET buttons and several plain HTML buttons. Anytime there's a textbox where a user hits enter, the ASP.NET button tries to submit the form.

I know I can change the defaultButton, but I don't want there to be any default button. I just want it so when the user presses enter it doesn't do anything.

I've tried setting defaultButton to blank, but that doesn't seem to work. How do I prevent the form from being submitted by the ASP.NET button when enter is pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

此生挚爱伱 2024-08-12 22:25:01

您可以设置按钮的 UseSubmitBehavior = false

btnCategory.UseSubmitBehavior = false;

You can set the button's UseSubmitBehavior = false

btnCategory.UseSubmitBehavior = false;
蒗幽 2024-08-12 22:25:01

这是我用来解决这个问题的方法。

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />

Here is what I used to fix this problem.

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
独孤求败 2024-08-12 22:25:01

我也遇到了这个问题,我的 MasterPage 上的 ImageButton(注销按钮)被设置为默认按钮(因此每当有人按下 Enter 时,它就会将其注销)。我通过在每个子页面的 Page_Load 事件中使用以下行解决了这个问题,这是一种解决方法,但它有效:

Form.DefaultButton = cmdSubmit.UniqueID;

希望这对其他人有帮助。

I also had this problem with an ImageButton on my MasterPage (the Logout button) being set as the default button (so whenever someone pressed Enter, it would log them out). I solved it by using the following line in the Page_Load event on every child page, which is a bit of a work-around, but it works:

Form.DefaultButton = cmdSubmit.UniqueID;

Hope this helps someone else.

雪花飘飘的天空 2024-08-12 22:25:01
<asp:TextBox ID="TXT_Quality" runat="server" Width="257px" 
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>   

function key_Pressed(e, textarea)
 {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) 
    { 
       return false;
    }
    return true;
 }
<asp:TextBox ID="TXT_Quality" runat="server" Width="257px" 
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>   

function key_Pressed(e, textarea)
 {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) 
    { 
       return false;
    }
    return true;
 }
时光匆匆的小流年 2024-08-12 22:25:01

如上所述将登录表单的“登录”按钮设置为默认按钮后,我在导航到另一个页面时遇到错误。我的修复方法是在母版页的加载事件中进行测试,如果包含的页面不是我的登录页面,则将默认值设置回如下所示:

if (Page.AppRelativeVirtualPath != "~/Login.aspx") {
    Page.Form.DefaultButton = "";
}

I was getting an error when navigating to another page after having set my login form's Sign In button as the default as described above. My fix was to test in the Master Page's load event and if the contained page was not my login page, to set the default back to nothing like this:

if (Page.AppRelativeVirtualPath != "~/Login.aspx") {
    Page.Form.DefaultButton = "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文