我可以创建一个不回发的 ASP.NET ImageButton 吗?

发布于 2024-08-30 06:21:03 字数 146 浏览 11 评论 0原文

我尝试仅使用 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 技术交流群。

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

发布评论

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

评论(8

初心 2024-09-06 06:21:03

我知道这个问题已经得到解答,但一个简单的解决方案是从 HTML onclick 方法(即 ASPX OnClientClick 方法)返回 false,例如

<asp:ImageButton ID="ImageNewLink" runat="server" 
 ImageUrl="~/images/Link.gif" OnClientClick="DoYourStuff(); return 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.

<asp:ImageButton ID="ImageNewLink" runat="server" 
 ImageUrl="~/images/Link.gif" OnClientClick="DoYourStuff(); return false;"  />

Returning false stops the browser from making the request back to the server i.s. stops the .NET postback.

守望孤独 2024-09-06 06:21:03

下面是一种在不与其他控件的回发功能发生冲突的情况下实现此目的的方法:

定义您的按钮,如下所示:

<asp:Button runat="server" Text="Button" UseSubmitBehavior="false" OnClientClick="alert('my client script here');my" />

OnClientClick 处理程序中以“my”结尾的方法是为强制回发的 asp.net 的 __doPostBack 客户端事件指定别名的一种方法;我们只是通过不执行与此脚本类似的任何操作来覆盖该行为:

<script type="text/javascript">

function my__doPostBack(eventTarget, eventArgument) {
    //Just swallow the click without postback of the form
}
</script>

编辑:是的,我觉得在我需要使用一些肮脏的技巧之后我需要洗澡才能让 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:

<asp:Button runat="server" Text="Button" UseSubmitBehavior="false" OnClientClick="alert('my client script here');my" />

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:

<script type="text/javascript">

function my__doPostBack(eventTarget, eventArgument) {
    //Just swallow the click without postback of the form
}
</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.

南渊 2024-09-06 06:21:03

另一个解决方案是定义一个不执行任何操作的 PostBackUrl

<asp:imagebutton runat="server" PostBackUrl="javascript:void(0);" .../>

Another solution would be to define a PostBackUrl that does nothing

<asp:imagebutton runat="server" PostBackUrl="javascript:void(0);" .../>
何以笙箫默 2024-09-06 06:21:03
<image src="..." onclick="DoYourThing();" />
<image src="..." onclick="DoYourThing();" />
心欲静而疯不止 2024-09-06 06:21:03

使用服务器端图像控件

<asp:Image runat="server" .../>

很确定您可以向其中添加客户端 onclick 事件。

Use a server side Image control

<asp:Image runat="server" .../>

Pretty sure you can add the client onclick event to that.

睫毛溺水了 2024-09-06 06:21:03

解决方案 1

<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg"
OnClientClick="return false;"  />


解决方案2

<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg" 
OnClientClick="yourmethod(); return false;"  />

另外(解决方案2),您的javascript方法可能

<script type="text/javascript">
    function yourmethod() {
    __doPostBack (__EVENTTARGET,__EVENTARGUMENT); //for example __doPostBack ('idValue',3);
    }
</script>

在代码隐藏中采用这种形式

protected void Page_Load(object sender, System.EventArgs e)
{
    if (this.IsPostBack) {
        string eventTarget = this.Request("__EVENTTARGET") == null ? string.Empty : this.Request("__EVENTTARGET");
        string eventArgument = this.Request("__EVENTARGUMENT") == null ? string.Empty : this.Request("__EVENTARGUMENT");
    }
}

Solution 1

<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg"
OnClientClick="return false;"  />

OR
Solution 2

<asp:ImageButton ID="btn" runat="server" ImageUrl="~/images/yourimage.jpg" 
OnClientClick="yourmethod(); return false;"  />

In addition (solution 2), your javascript method may be in this form

<script type="text/javascript">
    function yourmethod() {
    __doPostBack (__EVENTTARGET,__EVENTARGUMENT); //for example __doPostBack ('idValue',3);
    }
</script>

in code behind

protected void Page_Load(object sender, System.EventArgs e)
{
    if (this.IsPostBack) {
        string eventTarget = this.Request("__EVENTTARGET") == null ? string.Empty : this.Request("__EVENTTARGET");
        string eventArgument = this.Request("__EVENTARGUMENT") == null ? string.Empty : this.Request("__EVENTARGUMENT");
    }
}
李白 2024-09-06 06:21:03

这对我来说非常有用:

使用 OnClientClick 编写脚本并使用 PostBackUrl="javascript:void(0);" 来避免回发。

<div class="close_but">
    <asp:ImageButton ID="imgbtnEChartZoomClose" runat="server" ImageUrl="images/close.png" OnClientClick="javascript:zoomclosepopup();" PostBackUrl="javascript:void(0);" />
    </div>

This works Great for me:

Use OnClientClick to write your script and PostBackUrl="javascript:void(0);" to avoid postback.

<div class="close_but">
    <asp:ImageButton ID="imgbtnEChartZoomClose" runat="server" ImageUrl="images/close.png" OnClientClick="javascript:zoomclosepopup();" PostBackUrl="javascript:void(0);" />
    </div>
简美 2024-09-06 06:21:03

使用 OnClientClick 编写脚本和 PostBackUrl="javascript:void(0);"以避免回发

Use OnClientClick to write your script and PostBackUrl="javascript:void(0);" to avoid postback

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文