取消ASP.NET中的默认提交按钮
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以设置按钮的 UseSubmitBehavior = false
You can set the button's UseSubmitBehavior = false
这是我用来解决这个问题的方法。
Here is what I used to fix this problem.
我也遇到了这个问题,我的 MasterPage 上的 ImageButton(注销按钮)被设置为默认按钮(因此每当有人按下 Enter 时,它就会将其注销)。我通过在每个子页面的 Page_Load 事件中使用以下行解决了这个问题,这是一种解决方法,但它有效:
希望这对其他人有帮助。
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:
Hope this helps someone else.
如上所述将登录表单的“登录”按钮设置为默认按钮后,我在导航到另一个页面时遇到错误。我的修复方法是在母版页的加载事件中进行测试,如果包含的页面不是我的登录页面,则将默认值设置回如下所示:
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: