JavaScript - To enable and disable linkbutton using javascript

Asked By abinav shankar on 14-Aug-12 05:43 AM

Hi

I want to enable and disable link button using javascript

I have used the following code but its of no use any help will be valued the most

To enable:
document.getElementById(clientID).disabled = "";
To disable:
document.getElementById(clientID).disabled = "disabled";

Danasegarane Arunachalam replied to abinav shankar on 14-Aug-12 06:14 AM
Hope this helps

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
    function validate() {
      var check = document.getElementById('cbEnable');
      var lnk = document.getElementById('lnkEnable');
      if (!check.checked) {
        lnk.disabled = true;
        lnk.removeAttribute("href");
      }
      else {
        lnk.disabled = false;
        lnk.setAttribute("href", "http://techreceipe.com");
      }
        
    }
  </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   Enable/Diable Link Button :<input type="checkbox" onclick="validate()" id="cbEnable" />
  <asp:LinkButton ID="lnkEnable"  runat="server" PostBackUrl="~/Default.aspx">Link Button</asp:LinkButton>
    </div>
    </form>
</body>
</html>

ajay chauhan replied to abinav shankar on 14-Aug-12 06:20 AM
Hello Abhinav,

You have to first define an id for your link button then use the following code to disable the linkbutton using javascript.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript">
        function DisableLinkButton() {
            document.getElementById("<%=lb1.ClientID%>").disabled = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton runat="server" ID="lb1" PostBackUrl="http://www.yahoo.com" Visible="true" Text="Yahoo"></asp:LinkButton>
        <script language="javascript">
            DisableLinkButton();
        </script>
        
    </div>
    </form>
</body>
</html>
Thanks,
Ajay