5

I have a link button in the page like:

<asp:LinkButton ID="edit" runat="server" OnClick="edit_Click" Enabled="False">ویرایش</asp:LinkButton>

I want Enable/Disable this in javascript.

I use this code but set visible

var objedit = document.getElementById('<%= edit.ClientID.ToString() %>');
objedit.style.display = "none";

I use this code but not enable

if (count == 1) {
    objedit.disabled = false;
} else {
    objedit.disabled = true;
}

I can click but link button is disabled.

enter image description here

7
  • what you want ? disable or hidden the element ? Commented Apr 8, 2012 at 5:53
  • display the element . I want show 'Edit Item' like 'New Item' Commented Apr 8, 2012 at 5:56
  • hey pesar , farsi begoo , motavajeh nashodam manzooret chie? Commented Apr 8, 2012 at 6:02
  • man ye linkbutton daram ke mikham enable/disable konam az hame in coda ham estefade kardam vali hamintor ke toie ax mibinin be sorat disable neshon mide , albate mitonam click konam vali karbar be sorat gheir faal mibine :) Commented Apr 8, 2012 at 6:09
  • Check out my updated example. Commented Apr 8, 2012 at 6:31

5 Answers 5

1

This link should give you everything you need. You can't really "disable" a linkbutton because its just a link with some javascript attached. Basically, you need to reassign the click handler to something that returns void or false.

You can refer to the ID of the link with the following script:

<script runat="server">
   var a = document.getElementById('<%= edit.ClientID.ToString() %>')
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

@argorgin: Did you look at the links I posted? You need to reassign the onclick event handler of the linkbutton: a.onclick = function(){};
I set Attribute for linkbutton (onclick).
1

So, is this what you want?
http://jsfiddle.net/hzaR6/
http://jsfiddle.net/hzaR6/2/ -- UPDATED, tested in Chrome and Firefox

The UPDATED way

You can use a class name to define disabled element, for which you can have more control on their styles ... etc.

$("#link").toggleClass("disabled"); //This will simply toggle the class

and for the css

#link.disabled{
    z-index:-1;            /*Make it not clickable*/
    position:relative;
    opacity: .5;           /*Lighter*/
}​

You can do whatever you want here.

The good old form element way

$("#edit").attr("disabled", false);
 -or-
document.getElementBy("edit").disabled = false;

This will disable any form element. If you want to enable them, just change false to true.


var a = document.getElementBy("edit").disabled;

a will be true if the element is disabled. Otherwise it will be false.

2 Comments

I had the same thought, but this solution doesn't work on asp.net link buttons (they are just hyperlinks with javascript attached to their onclick handlers) see here: jsfiddle.net/hzaR6/1.
@Derek - you're right. If the OP wants to disable a linkbutton via javascript, he either needs to change the onclick event or the linkbutton itself, or use an asp:button instead (in which case, your solution would work).
1

To disable element:

document.getElementById('<%# edit.ClientID %>').disabled = 'disabled';
//.ToString() is not necessary; ClientID is a string.

To re-enable:

document.getElementById('<%# edit.ClientID %>').disabled = '';

Of course, it can be done after document (DOM) is loaded.

Comments

0

try this

var objedit = document.getElementById("edit"); //not editid (editid is a variable)
objedit.disabled = true; //if I'm not mistaken its true/false or disabled

5 Comments

I use this code but linkbutton is disable . I mean, the buttons on the form is displayed.
Ok , i use this code . i can click on button but linkbutton is displayed.
wait man, i don't seemed to understand what you wanted to do, can you please clarify your question?
Plz show picture. I use this code(objedit.disabled = false;). but Edit Item is displayed .it should display like 'New Item'
you don't disabled it you remove it "(objedit.disabled == false) ? NewItem.style.display = none : NewItem.style.display = block; "where your NewItem is the Id of your New Item." .. I really don't get what are you trying to do
0
document.getElementById("lnk").style.display = "none";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.