Bob Smith
Bob Smith

Reputation: 841

Disabling a checkbox in asp.net

I am dynamically creating a CheckBox and setting the disabled property like this:

chk.Disabled = false.

This renders the following HTML:

<input type="checkbox" disabled="disabled" .../>

On the click of the checkbox, I have a JS function that tries to enable it by doing a:

//get reference to the checkbox
chk.disabled = false;

This does not work. Any help?

Upvotes: 0

Views: 2905

Answers (3)

Antony Scott
Antony Scott

Reputation: 21978

What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.

Try the below to see what I mean.

<html>
<body>
    <input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
    <label for="cb">Click me</label>

    <input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>

Hope that helps you out.

Upvotes: 1

David Basarab
David Basarab

Reputation: 73301

How are you dynamically creating the check box?

Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.

What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.

Upvotes: 0

Allen Rice
Allen Rice

Reputation: 19446

If your checkbox is disabled, your onclick wont be called

Upvotes: 1

Related Questions