我可以创建一个不回发的 ASP.NET ImageButton 吗?
我尝试仅使用 ImageButton 控件来执行客户端脚本。我可以使用 OnClientClick 属性指定要执行的客户端脚本,但如何阻止它在用户每次单击它时尝试发布?单击此按钮后没有理由发帖。我已将 CausesValidation 设置为 False,但这并不能阻止它发布。
I'm trying to use the ImageButton control for client-side script execution only. I can specify the client-side script to execute using the OnClientClick property, but how do I stop it from trying to post every time the user clicks it? There is no reason to post when this button is clicked. I've set CausesValidation to False, but this doesn't stop it from posting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这个问题已经得到解答,但一个简单的解决方案是从 HTML onclick 方法(即 ASPX OnClientClick 方法)返回 false,例如
返回 false 会阻止浏览器向服务器发出请求,从而停止 .NET 回发。
I know this problem has already been answered but a simple solution is to return false from the HTML onclick method (i.e. the ASPX OnClientClick method) e.g.
Returning false stops the browser from making the request back to the server i.s. stops the .NET postback.
下面是一种在不与其他控件的回发功能发生冲突的情况下实现此目的的方法:
定义您的按钮,如下所示:
OnClientClick 处理程序中以“my”结尾的方法是为强制回发的 asp.net 的 __doPostBack 客户端事件指定别名的一种方法;我们只是通过不执行与此脚本类似的任何操作来覆盖该行为:
编辑:是的,我觉得在我需要使用一些肮脏的技巧之后我需要洗澡才能让 asp.net 做我想做的事情。
Here's one way you could do it without conflicting with the postback functioning of other controls:
Define your button something like this:
The "my" ending in the handler for OnClientClick is a way to alias asp.net's __doPostBack client event that forces the postback; we simply override the behavior by doing nothing similar to this script:
Edit: Yeesh, I feel like I need to take a shower after some of the dirty tricks that I need to pull in order to get asp.net to do what I want.
另一个解决方案是定义一个不执行任何操作的 PostBackUrl
Another solution would be to define a PostBackUrl that does nothing
使用服务器端图像控件
很确定您可以向其中添加客户端
onclick
事件。Use a server side Image control
Pretty sure you can add the client
onclick
event to that.解决方案 1
或
解决方案2
另外(解决方案2),您的javascript方法可能
在代码隐藏中采用这种形式
Solution 1
OR
Solution 2
In addition (solution 2), your javascript method may be in this form
in code behind
这对我来说非常有用:
使用
OnClientClick
编写脚本并使用PostBackUrl="javascript:void(0);"
来避免回发。This works Great for me:
Use
OnClientClick
to write your script andPostBackUrl="javascript:void(0);"
to avoid postback.使用 OnClientClick 编写脚本和 PostBackUrl="javascript:void(0);"以避免回发
Use OnClientClick to write your script and PostBackUrl="javascript:void(0);" to avoid postback