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.

2 comments:

  1. Hi its very useful.

    ReplyDelete
  2. Hi..Thanks a lot for this information. But its not working in Mozilla Firefox. I mean, Linkbutton color is not changing to Gray color. Can you please help me to disable a linkbutton in Firefox?..
    Thanks in advance for your help.

    ReplyDelete

Comments posted on ASP.Net Ajax Tutorials Blog are moderated and will be approved only if they are on-topic and not abusive. Please email me or my team for tech-support or blogging related questions. Avoid including website URLs in your comments - Thanks Author