Archive for April, 2008

Have a form that has multiple repeated submissions? Ever wonder why that happens? The most possible reason for multiple form submissions is a user double clicking a submit button. This results in the form being submitted twice. There have been several ideas in how to prevent this, but the easiest way is to simply add javascript to the onclick action. Doing this in C# is extremely easy. Simply use the following script, where exampleButton is the id of a LinkButton in C#.

exampleButton.Attributes.Add(”onclick”, “this.disabled=true;”);

An explanation to what the Attributes.Add function does is add an attribute to the exampleButton. This attribute is the onclick action. On each click, the javascript function this.disabled=true disables any future clicks.

In javascript, the ‘this‘ keyword refers to the control that called it. In this case, ‘this‘ refers to the exampleButton. Therefore, this javascript code snippet sets the disabled attribute to true, instantly disabling future button clicks.