javascriptc#asp.netasplinkbutton

How to enable LinkButton after 10 seconds Asp.net?


When I click on LinkButton lbUpVoteUndo it should disable for 10 seconds. I have tried it in c# code-behind. Is there another way to do same thing?

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

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

Solution

  • Javascript can do this for you. These two lines of code will do it for you:

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

    It's been awhile since I worked on .NET webforms but in your control I believe you could do something like this:

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

    I'd like to add that 10 seconds is a long time, are you expecting the user to wait around for 10 seconds? For instance if the user hits refresh the button will become enabled again. If you want to keep it disabled for 10 seconds no matter what happens, then you should probably store a flag in Session or Cache which can then set a client side element with the timer perhaps.