Disable anchor tag and link button onclick using javascript or code behind

Targets:
  • Disable anchor tag onclick event using javascript
  • Disable asp link button onclick using javascript
  • Prevent asp link button double click using javascript

Solutions:

Disable onclick event of a link button or an anchor tag using javascript

First of all you should know that asp link button is rendered as anchor tag.
So, if you want to disable onclick event of a link button or an anchor tag using javascript, you should set the onclick event as follows: 
For anchor tag:
 
<a href="somepath.com" onclick="return false" href="http://code-clue.blogspot.com/" />

For link button: 

<asp:LinkButton runat="server"  OnClientClick="return false;"  PostBackUrl="http://code-clue.blogspot.com/"  />

Or

<asp:LinkButton runat="server" Enabled="false" PostBackUrl="http://code-clue.blogspot.com/"  />






Disable onclick event of a link button from code behind
 
Suppose you have a linkbutton called myLinkButton:

<asp:LinkButton id="myLinkButton"/>
To disable the linkbutton onclick event form code behind:

 myLinkButton.Attributes.Add("onClick", "return false;");

Or

myLinkButton.Enabled=false;





How to prevent double click on link button using java script?

The idea behind the answer to this question is very simple and based on the way to disable the link button using java script as described above. The way to do this is to let the user click on the link button once and then set onclick to "return false".
First, create the following javascript function:

<script type="text/javascript">function disableClicks(element)
{
    element.onclick = function() { return false; };
}
</script>

What this fuction actualy does is to set the onclick event of  the element passed as parmater to reaurn false, and thus preventing the element from being furtherly clicked.
The only thing that's left to do is to set the onclick event to call the javascript function and to pass the link button or the anchor tag element as paramer:
For link button:

<asp:LinkButton runat="server"   OnClientClick="disableClicks(this);" PostBackUrl="http://code-clue.blogspot.com/"  />






How to prevent double click on anchor tag using java script?


The same as described above for link button;

<a href="somepath.com" onclick="disableClicks(this);"  href="http://code-clue.blogspot.com/" />
















9 comments:

vaas said...

thxn!

Unknown said...

I just wanna say thank you for sharing the content and wish you all the best for your website and your whole team.

Shella
www.imarksweb.org

Unknown said...


There are several ways to Disable the anchor element href . 3 Simple Methods of 'How to Disable Anchor Tag' in a Very Small Line of Code is given in the link below

http://techniblogic.com/how-to-disable-anchor-tag-href-links-by-html-css-javascript/
MUST CHECK
It is the simplest way i have founded to disable the anchor tag

Jennifer Robinson said...

This is an ultimate post. I was looking for this kind of a thing on the internet and now I found it. This blog helps my guest blogging site. Thanks

radha said...

Nice post .Keep sharing UI online course Bangalore

rabekahmarshall said...

Amazing post.
If you are finding dissertation writing services then go for the best writing platform available online.

Anonymous said...

Tül perde modelleri
sms onay
mobil ödeme bozdurma
Nft Nasıl Alinir
ankara evden eve nakliyat
trafik sigortası
Dedektor
Website.kurma
aşk kitapları

Anonymous said...

tuzla samsung klima servisi
ümraniye vestel klima servisi
kartal bosch klima servisi
pendik alarko carrier klima servisi
kadıköy toshiba klima servisi
maltepe beko klima servisi
beykoz arçelik klima servisi
ataşehir samsung klima servisi
çekmeköy mitsubishi klima servisi

Anonymous said...

uc satın al
nft nasıl alınır
lisans satın al
minecraft premium
yurtdışı kargo
özel ambulans
en son çıkan perde modelleri
en son çıkan perde modelleri

Post a Comment