Canceling Linkbutton Clicked Event when the User tries to click it Again

There is many reasons to prevent that , like preventing mutiple Database Calls , Or Even Preventing them from submitting the form twice and so save the bandwidth and server resources .

this is a one solution for that Issue ,

Use a hiddenField control to remeber the Click counts ,and when the Linkbutton clicked you check to see if the click counts >0 ,

 if yes then you will cancel the click event ,

To Accomplish this , Add a hiddenField Server control to your page as follows :

<asp:HiddenField ID="HiddenField1" runat="server" Value="0" />
Now Register Onclick Attribute for the Linkbutton , in page load add this code :
 If Not IsPostBack Then
   LinkButton1.Attributes("onclick") = String.Format("javascript:var Count = document.getElementById('{0}');
 if (Count.value>0) return false ;Count.value =Count.value+1; ", HiddenField1.ClientID)
 End If

How To Test that :

Add the hidden field and register the Onclick attribute as mentioned ,
Now click the LinkButton for the first time , the form must be submitted (postback must occured)
Try to click the LinkButton for the second time, you will notice that there is no postback happened .

Anas ghanem

1 Comment

Comments have been disabled for this content.