Monday, May 25, 2009

How to enable or disable LinkButton in Javascript.

we can make asp.net Linkbutton Enable\Disable in Javascript. normally disabled='disabled' attribute works only in internet explorer not in Mozilla. to resolve this problem we can develope a javascript function Which perform this.

(1) Suppose we have a LinkButton

<asp:LinkButton ID="lnkTest" runat="server" CommandArgument="1" CommandName="1x"
OnClick="lnkTest_Click">Test</asp:LinkButton>


(2)Style for enabling and Disabling is

<style>
.LnkEnabled
{
cursor: pointer;
}
.LnkDisabled
{
cursor: default;
color: Gray;
}
</style>


(3) and javascript fiuction is

<script language="javascript">
function EnableLinkButton(ID,flag)
{
document.getElementById(ID).onclick=function(){return flag;};
if(!flag)
{
document.getElementById(ID).setAttribute("disabled","disabled");
document.getElementById(ID).className="LnkDisabled";
}
else
{
document.getElementById(ID).setAttribute("disabled","");
document.getElementById(ID).className="LnkEnabled";
}
}
EnableLinkButton('<%= lnkTest.ClientID %>',false);
</script>


Calling above javascript function we can Enable or disable LinkButton from Javascript.

Thanks.