如何在浏览器处理请求时禁用开始按钮

How to Disable Start Button While Browser Processing the Request

本文关键字:开始 按钮 请求 浏览器 处理      更新时间:2023-09-26

我有开始和停止按钮我用它来启动和停止服务器上的窗口服务
当我点击启动按钮服务开始,但它需要一些时间来启动服务在该时间范围内意味着启动服务。我必须禁用客户端的开始按钮,与禁用停止按钮相同。

当按钮被禁用时,我使用了下面的代码

javascript

 function btnStartClick()
 {
 $("input[ID=btnStart]").attr("disabled", "disabled");
 }
c#

protected void btnStart_Click(object sender,EventArgs e)
{
//service starst code here
}

标记
 <asp:Button ID="btnStart" runat="server" Text="Start" OnClientClick="return btnStartClick();" OnClick="btnStart_Click" />
$('#<%= btnStart.ClientID %>').attr("disabled", "disabled");

如果我没理解错的话。

你可以发送ajax请求到asp页面,你有你的服务启动逻辑和禁用按钮在这一刻,之后检查你的ajax响应,如果你的服务启动你启用按钮,如果没有显示消息或以任何其他方式处理它。

如果你只问如何使用jQuery禁用按钮,请查看humpty dumptybipen答案。

为什么不用" # "…而不是属性选择器。这比您使用的

更快更有效。
$('#<%= btnStart.ClientID %>').attr("disabled", "disabled");

他的问题不是他不能禁用按钮,而是他想要他的OnClick事件着火。他的按钮单击事件不会为被禁用的按钮触发。

如果你想阻止以后的点击,你可以隐藏按钮,然后在代码后面禁用它:

<script>
    function btnStartClick() {
        $("#<%= btnStart.ClientID %>").hide();
        return true;
    }
</script>

和后面的代码:

protected void btnStart_Click(object sender, EventArgs e)
{
     btnStart.Enabled = false;
     //service starts code here
}

您有两个选择:

$('#<%= btnStart.ClientID %>').prop("disabled", true);

使用ASP。. NET选择OR:

$("input[id$=btnStart]").prop("disabled", true);

每个ID以btnStart结尾的输入,当控件不在作用域中时,这有时很有用。

正如另一条评论所指出的,一旦按钮被禁用,它可能不会触发onClick后面的代码,所以在这种情况下,最好隐藏/只读按钮,而不是禁用它。

谢谢你的回复,我得到了解决方案

<asp:Button ID="btnStart" runat="server" Text="Start" OnClientClick="this.disabled = true; this.value = 'Please Wait';"
            UseSubmitBehavior="false" OnClick="btnStart_Click" />