-2

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;
    }
}
  • I'm going to assume this is WebForms, since you haven't specified. If you're trying to load the page, then 10 seconds later have the button enable, this will not be possible with server code. The code you've posted will wait 10 seconds to execute, then the page will be loaded with the button enabled or disabled. – Seano666 Jun 16 '17 at 15:44
  • Its not related to `WebForms`. It is simple `linkbutton` to which I click. –  Jun 16 '17 at 15:47
  • 3
    I'd recommend doing this with javascript if you can, otherwise, you're gonna need to wrap that button in an UpdatePanel and I'm not sure if you have that implemented. – Jack Marchetti Jun 16 '17 at 15:51
  • 3
    @Asif.Ali - you could try looking it up as well. You might learn something. – Kixoka Jun 16 '17 at 17:12

2 Answers2

2

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.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
0

Use javascript function -

In HTML, Add below code with your button control -

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

In .CS cdoe, Use this in you button click event -

System.Threading.Thread.Sleep(10000);

Thanks :)