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
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;
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 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/" />