1

I use VS2010,C# to develop my ASP.NET web app, I've created an asp.net hyperlink like this:

<asp:HyperLink ID="hpAccept" runat="server" Enabled="false" Target="_parent">Hyperlink</asp:HyperLink>

as you can see I've defined it as enabled=false, so it is disabled at startup, I've defined a JavaScript function that should enable this hyperlink after a variable is set to true, but it doesn't work! what is my problem, it is my JS function:

           function onRadioChange(rowIndex, value) {

.....
....
           if (all_ok) {
               document.getElementById('hpAccept').disabled = false;
           }
       }

my variable (all_ok) is set to true but the hyperlink is not enabled!

thanks

2
  • Is this javascript code is inside the page itself? Jan 12, 2012 at 8:44
  • yes this function is inside the page, this function performs something more also, and they all do correctly but this line doesn't do anything!
    – Ali_dotNet
    Jan 12, 2012 at 8:46

3 Answers 3

2

The element's id will not be 'hpAccept'

instead use:

document.getElementById('<%=hpAccept.ClientID%>').disabled = false;
3
  • have you made sure to return false from any click function to prevent the default postback?
    – Skyrim
    Jan 12, 2012 at 8:51
  • as I told this function is working fine, but this line doesn't answer as expected, what you mean exactly by click function? of course no postback occurs, but I think that might be a problem, how should I prevent postback?
    – Ali_dotNet
    Jan 12, 2012 at 8:53
  • for example, if you hook a onclick function then you should do the following: OnClickFunction() { ...; return false; }
    – Skyrim
    Jan 12, 2012 at 8:59
1

<%= hpAccept.ClientID %> should work but not in a separate javascript file where server side scripts do not execute.

Another possibility is to use a class selector: jQuery

<input runat="server" id="hpAccept" value="test" class="txtTest" />

and then:

var value = $('.txtTest').removeAttr('disabled'); //Updated

Hope this helps.

2
  • Hmmm jQuery is a java script library. Anyhow in page source on client machine search hpAccept, you should able to find it at two places, the actual control and your javascript. Jan 12, 2012 at 8:58
  • this is not XHTML-conform ... either use .attr('disabled', 'disabled') (to disable it) or .removeAttr('disabled') (to enable it)
    – user57508
    Jan 13, 2012 at 6:40
0

You should add to your HyperLink markup attribute ClientIDMode with value static, it must be looks like this

<asp:HyperLink ID="hpAccept" runat="server" Enabled="false" ClientIDMode="Static" Target="_parent">Hyperlink</asp:HyperLink

After that, you can use jquery

$("#hpAccept").attr("disabled", "disabled");
2
  • -, hpAccept is not the rendered id (aka clientID) ... it will get "parented" by the parent-control-ids ... you will get the correct it, by using <%= this.hpAccept.ClientID %>
    – user57508
    Jan 12, 2012 at 9:02
  • thanks friend, I could solve my problem, I just added HREF to my hyperlink:document.getElementById('hpAccept').disabled = false; document.getElementById('hpAccept').href = "google.com";
    – Ali_dotNet
    Jan 12, 2012 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.