0

I have a problem in disabling the button in ASP.Net

I cannot disable the button when there is a loop

example of my code:

private void button1(object sender, EventArgs e)
{
    button1.Enabled = false;

    for(int i = 0; i < 1000000; i++)
    {
         for(int j = 0; j < 1000000; j++)
         {
            for(int k = 0; k < 1000000; k++)
            {

            }
         }
    }
}

I still need to wait to finish the execution of the for loop before the button will be disable.. What should I need to do to disable my button before the execution of the for loop?

portgas d ace
  • 309
  • 3
  • 9
  • 21

2 Answers2

1

Your best bet is to use a little snippet of javascript code to disable the button, before the form posts back. The way you have it it won't work. I assume you are trying to prevent the user from clicking the button more than once, while the long-running operation takes place.

Something like this:

<INPUT name="myButton" type="submit" onclick="document.form1.myButton.disabled=true;document.form1.submit();">

If you are already using jquery, there are better ways as well.

E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
0

You are actually missing the basic concept of ASP.Net Page Life Cycle. The button will only get disabled when the response will be returned from the server (which is obviously after the loop completion). Now, if you want to disable it then you will have to use Javascript or Jquery as E.J Brenan suggested.

Page Life Cycle: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

Behroz Sikander
  • 3,885
  • 3
  • 22
  • 36