Virtually every asp.net developer has met the situation when a page postback takes quite some time (this is often related to web service calls at back end) and it is required to prevent user from submitting the form twice. The task sounds easy, doesn’t it? Unfortunately there is no easy and straighforward way in ASP.NET to accomplish this.

The main problem is a default ASP.NET postback behaviour when a disabled html button (the techique is – to disable button straight after clicking) cannot submit the form. Other techiques like using html buttons, hidden fields to pass value to the server side and javascript calls to __DoPostback() are pretty cumbersome and far from being elegant.

Luckily I’ve come across the following method which is both elegant and simple:

Code behind:


protected override void OnInit (EventArgs e)
{
  // We need this to disable our button when submitting
  Form.SubmitDisabledControls = true;
  btnSubmit.UseSubmitBehavior = false;

  base.OnInit(e);
}

Enjoy!

You may also like

Linq to SQL select and update oddity

Today i came across some very odd behaviour using Linq to SQL. I was trying to retrieve a record from the database to update one column then save the change to the database. The problem was happening when i tried to save the change. I was getting foreign constraint errors on related records that didn’t even exist!

Keep Reading

CSV parsing the easy way!

Every now and then we have a client that needs to import data regularly from one system to another and a lot of the time we go for the good old csv file. In the past i have always just written a simple function that reads each line of the file, splits it into cells and import it into the database. But this simple code never fully handles csv’s correctly.

Keep Reading

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.