Tuesday 4 December 2007

Ghost button alternative

Following on from my previous post about Ghost Buttons, I've now found a much easier and efficient alternative.

ASP.NET 1.x:
btnTest.Attributes.Add( "onclick", "javascript:this.disabled = true; " + GetPostBackEventReference( btnTest ) );

ASP.NET 2.0:
btnTest.Attributes.Add( "onclick", string.Format( "javascript:this.disabled = true; {0}", ClientScript.GetPostBackEventReference( btnTest, "" ) ) );

Advantages:

  • You only need a handle to the control instead of the ID - sometimes these can get complicated if controls are placed inside other controls

  • There is less code

  • Only a single control is ouputted - this could mean a performance increase compared to using Ghost Buttons

Disadvantages:

  • The code will need to be recompiled to make any changes to the click event

No comments: