单击按钮禁用回发
我想在单击
后禁用回发。我尝试通过分配 onclick="return false"
来做到这一点,但在按钮中不起作用。
我该如何解决这个问题?
I want do disable postback after clicking a <asp:Button>
. I've tried to do that by assigning onclick="return false"
, but in the button doesn't work.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
应该这样做!
Onclick
将引用 ASP.net 函数,onClientClick
将在 HTML 控件上呈现为OnClick
。That should do it!
Onclick
will refer to an ASP.net function,onClientClick
will render asOnClick
on the control in HTML.onclick
用于连接服务器端事件。您需要使用OnClientClick
处理程序,例如onclick
is used to wire up your server side events. You need to use theOnClientClick
handler such as<asp:button OnClientClick="return false;" />
我可以使用包含相关控件/元素的 UpdatePanel 来修复该问题。
I could fix that using an UpdatePanel which includes the implicated controls/elements.
它对我来说是这样的:
在
:
和
:
It worked for me like this:
In
<body>
:and in
<head>
:使用这个:-
Use this:-
将您的 asp 按钮放入 Updatepanel 中,例如
Put your asp button to inside the Updatepanel like
看来这些答案都没有帮助我。我找到了解决方案。您不应使用 ASP.NET 按钮
,而应使用 HTML 按钮。它工作得很好并且看起来与 ASP.NET 按钮一模一样。要让 HTML 按钮在服务器端提交,请使用属性onserverclick
。对于我的代码,我使用 JS 在服务器端执行某些操作。
不会停止回发,但正如我所说,HTML 按钮解决了我的问题。Seeing as none of these answers helped me. I found a solution. Instead of using the ASP.NET button
<asp:Button>
, you should use the HTML button. It worked perfectly and looks exactly like the ASP.NET button. To get the HTML button to submit on server side, you use the attributeonserverclick
.For my code, I was using JS to do something on the server side. The
<asp:Button>
would not stop doing a post back but as I said, the HTML button fixed my problem.既然您想在回发后执行此操作,我想您想防止双击回发?在这种情况下,您最好在第一次单击客户端后设置某种状态维护变量。作为一个简单的示例,
您然后设置 OnClientClick 在按钮上返回此方法结果,因此
OnclientClick="return AllowOneClick()"
当然,这仅适用于一个按钮,但它应该可以让您了解总体情况。
Since you want to do it after the postback, I presume you want to prevent double click postbacks? In this case you are best off having some sort of state maintaining variable that you set after the first click on the client side. As a simple example
You then set OnClientClick to return this method result on your button so
OnclientClick="return AllowOneClick()"
This will of course only work for one button, but it should give you the general idea.
就我而言,上述解决方案都不适合我。
我想要的是首先调用客户端的函数,然后停止向服务器的回发。我的解决方案是在调用客户端函数之前简单地添加
return
关键字,即:我的客户端脚本在最后一行代码中包含
return false
。In my case none of the solutions above worked for me.
What I wanted was to first call a function on the client side and then halt the postback to the server. My solution was to simply add the
return
keyword before calling my client-side function, i.e.:My client-side script contains
return false
in the last line of code.通过使用 UseSubmitBehavior="false" 您可以禁用 onclick 的回发
by using UseSubmitBehavior="false" you can disable postbacks of onclick