[英]How to disable button after click to avoid multiple post requests?
I want to disable a server side asp.net button control after click for some seconds & then fire a server side event. 我想在点击几秒后禁用服务器端asp.net按钮控件然后触发服务器端事件。 I am trying to use setTimeout but if I use setTimeout the server side event is never fired.
我正在尝试使用setTimeout,但如果我使用setTimeout,则永远不会触发服务器端事件。 Can anybody help?
有人可以帮忙吗?
<script type="text/javascript">
function disableBtn(btnId, dspTxt) {
var btn = document.getElementById(btnId);
btn.value = dspTxt;
setTimeout(btn.disabled = true,1000);
}
</script>
How about using jQuery? 使用jQuery怎么样?
$('#buttonId').attr("disabled", true);
or in a function it would be 或者在一个功能中
function disableBtn(btnId, dspTxt) {
var button = $(btnId);
button.attr("disabled", true);
button.text(dspTxt);
}
http://www.w3schools.com/jsref/prop_select_disabled.asp http://www.w3schools.com/jsref/prop_select_disabled.asp
Place two buttons on your form: 在表单上放置两个按钮:
<asp:Button ID="ButtonToDisable" runat="server" Text="Button" />
<asp:LinkButton ID="PostBackLinkButton" runat="server" onclick="PostBackLinkButton_Click"></asp:LinkButton>
First one is button that will be disabled after 2 seconds, second is button that will do the postback. 第一个是在2秒后将被禁用的按钮,第二个是将执行回发的按钮。
Next put this code inside page.cs: 接下来将此代码放在page.cs中:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ClientScript.RegisterStartupScript(this.GetType(), "timer",
"setTimeout(function(){" + Page.ClientScript.GetPostBackEventReference(PostBackLinkButton, String.Empty) + "},2000)", true);
}
}
protected void PostBackLinkButton_Click(object sender, EventArgs e)
{
ButtonToDisable.Enabled = false;
ButtonToDisable.Text = "Button is disabled!";
}
Now just run the page and wait for 2 seconds and postback will occur and disable PostBackLinkButton. 现在只需运行页面并等待2秒钟,就会发生回发并禁用PostBackLinkButton。
If you don't want that postback is visible to the user place buttons inside the updatepanel. 如果您不希望更新面板内的用户位置按钮可以看到回发。
If I correctly understand you need something like this: 如果我正确理解你需要这样的东西:
var isClicked=false;
$('.myButton').click(function (e) {
e=e?e:window.event;
if(!isClicked){
if($.browser.msie){e.cancelBubble=true; e.returnValue=false;}
else
e.preventDefault();
isClicked=true;
$(this).attr('disabled','disabled');
setTimeout(function(){delayClick();},1000);
}
});
function delayClick()
{
$(this).removeAttr('disabled');
$('.myButton').click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.