Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a requirement in my project which is being developed in C#.
I would like to enable/disable one asp.textbox based on the checking/Unchecking ASP:Checkbox.

Kindly let me know how to implement it (best way)?

Thanks
Posted
Updated 24-Jan-12 8:41am
v2

You can either do it using javascript, or you can use the onCheckChanged event on the checkbox and then handle the disabling of the textbox in the code-behind. I won't just give you the code for this as it is a very basic thing. Try it out and if you run into coding issues, update your question and we can help further.
 
Share this answer
 
v2
Comments
Espen Harlinn 24-Jan-12 15:58pm    
5'ed!
fjdiewornncalwe 24-Jan-12 16:49pm    
Thank you, Espen.
Sergey Alexandrovich Kryukov 24-Jan-12 23:57pm    
Agree, my 5. I only covered client-side variant. Teamwork!
--SA
fjdiewornncalwe 25-Jan-12 8:39am    
Thanks, Sergei
Tech Code Freak 25-Jan-12 2:30am    
5!
I thing, the best way would be doing it on the client side only (it this is acceptable; some sites are required to perform even it the user switched scripting off in the browser), because the postback for this simple operation would might take to much time to be reasonable. That said, you could do it in JavaScript; and one convenient way of doing it would be using jQuery.

To enable/disable, you can use this:
JavaScript
//enable:
$('#myTextBoxId').removeAttr('disabled');

//disable:
$('#myTextBoxId').attr('disabled', '');


You can handle the click event of the text, check if it is checked and enable or disable the text box accordingly:

JavaScript
$('#myCheckBox').click (function () {
    var thisCheck = $(this);
    if (thischeck.is (':checked')) {
        //enable text box here, see the code fragment above
    } else {
        //disable text box here, see the code fragment above
    }
});


Something like that.

Please see:
http://docs.jquery.com/Main_Page[^],
http://api.jquery.com/category/attributes/[^] (see how to work with attributes),
http://api.jquery.com/checked-selector/[^] (working with "checked").

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 24-Jan-12 15:57pm    
Good reply :)
Sergey Alexandrovich Kryukov 24-Jan-12 16:28pm    
Thank you, Espen.
--SA
fjdiewornncalwe 24-Jan-12 16:49pm    
Nice. I didn't want to give the OP something this complete, but I do like the answer. +5.
Sergey Alexandrovich Kryukov 24-Jan-12 16:51pm    
Thank you, Marcus.
Actually I've just done what I though was fastest for me to show in the answer.
--SA
Tech Code Freak 25-Jan-12 2:30am    
5up!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900