OnclientClick 和 OnClick 不能同时工作? [英] OnclientClick and OnClick is not working at the same time?

查看:34
本文介绍了OnclientClick 和 OnClick 不能同时工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的按钮,

I have a button like the following,

<asp:Button ID="pagerLeftButton" runat="server" OnClientClick="disable(this)" onclick="pager_Left_Click" Text="<" />

当我像这样使用我的按钮时,onclick 不会触发.当我删除 OnClientClick 时,onclick 正在触发.

When I use my button like that, onclick is not firing. When I remove OnClientClick, then onclick is firing.

我需要做的是,在回发期间禁用该按钮,并在回发结束后启用它.

What I need to do is, disable the button during the postback and enable it after the postback ends.

附加信息:

我在我的触发函数中添加了断点是 c# 部分并且我正在调试,它们肯定不会触发.那些功能就像

I added break point to my firing functions is c# part and I am debugging, they are not firing for sure. Those functions are like

protected void pager_Left_Click(object sender, EventArgs e)
{
//Do smthing.
}
protected void pager_Right_Click(object sender, EventArgs e)
{
//Do smthing.
}

当我点击我的按钮时,它会被禁用 1-2 秒并自动启用,但我不确定它为什么被启用.我没有添加任何部分以使其再次启用.

and when I click my button, it is disabled for 1-2 seconds and automatically enabled, but I am not sure why it is enabled. I didn't add any part for it to be enabled again.

推荐答案

从此web.archive.org 上的文章:

诀窍是使用按钮控件的 OnClientClick 和 UseSubmitBehavior 属性.还有其他方法,涉及到服务器端的代码来添加属性,但我认为这样做的简单性更有吸引力:

The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive:

<asp:Button runat="server" ID="BtnSubmit"  OnClientClick="this.disabled = true; this.value = 'Submitting...';"   UseSubmitBehavior="false"  OnClick="BtnSubmit_Click"  Text="Submit Me!" />

OnClientClick 允许您添加客户端 OnClick 脚本.在这种情况下,JavaScript 将禁用按钮元素并将其文本值更改为进度消息.回发完成后,新呈现的页面会将按钮恢复到其初始状态,无需任何额外工作.

OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

在客户端禁用提交按钮的一个陷阱是它会取消浏览器的提交,从而取消回发.将 UseSubmitBehavior 属性设置为 false 会告诉 .NET 注入必要的客户端脚本以触发回发,而不是依赖于浏览器的表单提交行为.在这种情况下,它注入的代码是:

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:

__doPostBack('BtnSubmit','')

这被添加到我们的 OnClientClick 代码的末尾,给我们呈现的 HTML:

This is added to the end of our OnClientClick code, giving us this rendered HTML:

<input type="button" name="BtnSubmit"  onclick="this.disabled = true; this.value ='Submitting...';__doPostBack('BtnSubmit','')"  value="Submit Me!" id="BtnSubmit" />

这提供了一个不错的按钮禁用效果和处理文本,同时回发完成.

This gives a nice button disable effect and processing text, while the postback completes.

这篇关于OnclientClick 和 OnClick 不能同时工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆