Check/uncheck checkboxes from javascript

D

Dilip

I am writing a WebControl that uses the status of a master check box
to determine if other dependent check boxes need to be checked/
unchecked. What is the best way to do it? I am not much of a web
programmer so apologies for the basic question.

Currently the way I see it being done is to generate individual
'javascript:' calls to a function on the master checkbox's onclick
handler, passing in the ID of the master and each dependent control
and flipping the latter based on the former. Is this the way to go?

IOW:

<....checkbox control HTML......
onclick="javascript:enableCB("master_ctrl","cb_1");enableCB("master_ctrl","cb_2") /
function enableCB(master, slave)
{
document.getElementById(slave).disabled = !
document.getElementById(master).checked;
document.getElementById(slave).parentNode.disabled = false; // I
don't even know why this is done :-(
}
 
T

tomisarobot

its one way to go.

a much simpler way is to set your checkboxes to AutoPostBack=true and
then handle it in the aspx.cs code behind.

you javascript gets a lot more complicated if you have your checkboxes
in a parent container that renames their ClientID, such as MasterPages
and any of the databound (datagrid/view/list/etc) type controls.

unless you are horribly concerned with bandwidth id just post back and
not worry about javascript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top