Click here to Skip to main content
15,898,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an ASP.Net page with a button.

I need to disable the button when it is clicked, process some code and when code is processed the button should be enabled back.

I wrote below code.

Button.enabled = false
/* code */
Button.enabled = true

but, it does not change the status of the button.

I also tried with JavaScript, it disables the button but doesnt process the code and reenable the button.

script.RegisterClientScriptBlock(Me.GetType(), "ButtonStatus", "document.getElementById('ButtonName').enables = false;")
/* code */
script.RegisterClientScriptBlock(Me.GetType(), "ButtonStatus", "document.getElementById('ButtonName').enables = true;")
Any suggestion on this guys...
Posted
Comments
Ankur\m/ 19-Nov-10 7:36am    
I think I am not understanding your problem correctly.
When you click the button, the page is posted back. The processing is done on the server side and the page is rendered. Now when you say you want to enable the button after processing, where does this processing actually happening? Are you talking about JavaScript processing or are you processing something on server side using AJAX. Could you please explain?

Thank you for your question.

C#
Button1.Enabled = false;
foreach (GridViewRow row1 in GridView1.Rows)
{
    CheckBox chk = (CheckBox)row1.FindControl("chkId");
    if (chk.Checked)
    {
       String strTem = chk.ID;
     }
}
Button1.Enabled = true;



I have used above code but did not find this problem as your question.

Thanks
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 19-Nov-10 7:35am    
@Abdul: Is your code of a WinForm application? Sifar wants to know how he can disable a button on web page.
Abdul Quader Mamun 19-Nov-10 7:56am    
It is in web form.
Have a look at this one

Disable Multiple Button Click in Asp.net[^]
 
Share this answer
 
Comments
TweakBird 19-Nov-10 12:58pm    
good call
JF2015 28-Nov-10 11:24am    
Good one.
Hello Sifar!

I take it that /* Code */ part is executed at the server side.
If this is true you wont see any changes on the button since enables = true
is set on the server side and when the page is displayed in the browser the button is enabled.


Cheers

Manfred
 
Share this answer
 
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       Button btn = sender as Button;
       btn.Enabled = !btn.Enabled;
       DoCode(btn);
   }
   protected void DoCode(Button sender)
   {
       //Code
       sender.Enabled = !sender.Enabled;
   }
 
Share this answer
 
Thank you all...

I have resolved the issue with below code in Load method.

Button1.Attributes("onclick") = String.Format("{0}.disabled=true;{1};", Button1.UniqueID, Page.ClientScript.GetPostBackEventReference(Button1, Button1.CommandArgument))
 
Share this answer
 
One more Logic you can apply for background processing using worker thread. It is more prominent to maintain state of button like in this manner

MIDL
BackgroundWorker Operation = new BackgroundWorker();
               Operation.DoWork += new DoWorkEventHandler(Operation_DoWork);
               Operation.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Operation_RunWorkerCompleted);
              Operation.RunWorkerAsync();



and in the event you can apply yours logic

 void Operation_DoWork(object sender, DoWorkEventArgs e){
  button1.enable = false;
// Apply your operation process
}


and if it is completed in background then implement this event

void Operation_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
  // Once your operation complete by background thread

  button1.enable = true;

}


I hope you like it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900