How to prevent multiple button clicks
-
I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.
btn_Program.Enabled = false;
I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:
static void DisableButtonDuringPostback(Page page, Button control)
{
StringBuilder sb = new StringBuilder();
sb.Append("this.disabled = true;");
sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
sb.Append(";");control.Attributes.Add("onclick", sb.ToString());
}
It said to add the following to Page_Load
DisableButtonDuringPostback(this.Page, Button3);
Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.
-
I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.
btn_Program.Enabled = false;
I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:
static void DisableButtonDuringPostback(Page page, Button control)
{
StringBuilder sb = new StringBuilder();
sb.Append("this.disabled = true;");
sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
sb.Append(";");control.Attributes.Add("onclick", sb.ToString());
}
It said to add the following to Page_Load
DisableButtonDuringPostback(this.Page, Button3);
Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.
Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
I have a c# .net project that has a button on the form. When the button is clicked twice, bad things happen. How do I disable the button? I've tried disabling my button as follows but it doesn't work.
btn_Program.Enabled = false;
I've searched for this online, but every solution seems to reference asp.net. I don't think I have that, do I? I'm fairly new to c# and visual studio, so I may have it and not know it. I liked the following discussion, but again, it refers to asp.net. http://stackoverflow.com/questions/8473261/how-to-disable-a-button-after-1st-click[^] The example I liked had the following code in it, but when I add it to my form class, it gets a compilation problem for ClientScript and Attributes:
static void DisableButtonDuringPostback(Page page, Button control)
{
StringBuilder sb = new StringBuilder();
sb.Append("this.disabled = true;");
sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString()));
sb.Append(";");control.Attributes.Add("onclick", sb.ToString());
}
It said to add the following to Page_Load
DisableButtonDuringPostback(this.Page, Button3);
Compilation problems: 1. 'System.Windows.Forms.Button' does not contain a definition for id and no extension method id accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?) 2. System.Windows.Forms.Button does not contain a definition for Attributes and no extension method Attributes accepting a first argument of type System.Windows.Forms.Button could be found (are you missing a using directive or an assembly reference?) Any help would be very much appreciated.
btn_Program.Enabled = false;
Are you sure that
btn_Program
is the correct button to disable? Since you aren't sure to have started an ASP.NET project, I will assume you have a Windows Forms project instead. That would mean that you have a button click event handling routine somewhere that looks similar to this:void btn_Program_Click(object sender, EventArgs e)
{
// ... your code here ...
}To definitely disable the correct button, you can use the
sender
parameter:void btn_Program_Click(object sender, EventArgs e)
{
// Disable the button whose Click event started this method
System.Windows.Forms.Button methodStartingControl = sender as System.Windows.Forms.Button;
if(methodStartingControl != null)
{
methodStartingControl.Enabled = false;
}// ... your code here ...
// Re-enable the previously-disabled button
if(methodStartingControl != null)
{
methodStartingControl.Enabled = true;
}
}Ciao, luker
-
btn_Program.Enabled = false;
Are you sure that
btn_Program
is the correct button to disable? Since you aren't sure to have started an ASP.NET project, I will assume you have a Windows Forms project instead. That would mean that you have a button click event handling routine somewhere that looks similar to this:void btn_Program_Click(object sender, EventArgs e)
{
// ... your code here ...
}To definitely disable the correct button, you can use the
sender
parameter:void btn_Program_Click(object sender, EventArgs e)
{
// Disable the button whose Click event started this method
System.Windows.Forms.Button methodStartingControl = sender as System.Windows.Forms.Button;
if(methodStartingControl != null)
{
methodStartingControl.Enabled = false;
}// ... your code here ...
// Re-enable the previously-disabled button
if(methodStartingControl != null)
{
methodStartingControl.Enabled = true;
}
}Ciao, luker
-
Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
Add a timestamp; if it comes in again, compare the current time to the datestamp you saved earlier. If it is <2 seconds, ignore and cancel execution of the request.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]