如何在 10 秒后启用 LinkBut​​ton Asp.net?

How to enable LinkButton after 10 seconds Asp.net?

当我点击 LinkBut​​ton lbUpVoteUndo 时,它应该禁用 10 秒。我已经在 c# 代码隐藏中尝试过了。还有其他方法可以做同样的事情吗?

protected void lbUpVoteUndo_Click(object sender, EventArgs e)
{
    lbUpVoteUndo.Enabled = false;

    if(/* check for 10 seconds */)
    {
        lbUpVoteUndo.Enabled = true;
    }
}

Javascript 可以为您做到这一点。这两行代码将为您完成:

function DisableFor10Seconds() {
      document.getElementById("<% lbUpVoteUndo.ClientID %>").disabled = true;
      setTimeout(function(){document.getElementById("<% lbUpVoteUndo.ClientID %>").disabled = false;},10000);
}

我使用 .NET webforms 已经有一段时间了,但在你的控制下,我相信你可以做这样的事情:

<asp:LinkButton ID="lbUpVoteUndo" OnClientClick="Disablefor10Seconds();" />

我想补充一点,10 秒是很长的时间,您是否希望用户等待 10 秒左右?例如,如果用户点击刷新按钮将再次启用。如果无论发生什么情况都希望将其禁用 10 秒,那么您应该在 Session 或 Cache 中存储一个标志,然后可以使用计时器设置客户端元素。

使用javascript函数-

在 HTML 中,使用您的按钮控件添加以下代码 -

UseSubmitBehavior="false" OnClientClick="this.disabled='true'; this.value='Please wait...';"

在 .CS cdoe 中,在您的按钮单击事件中使用它 -

System.Threading.Thread.Sleep(10000);

谢谢:)