• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 959
  • Last Modified:

Check all checkboxes in a treeview

In my ASP.Net 2.0 C# application, I have a page with two treeviews.
Each node of the treeviews has a checkbox.

I need a method of checking/unchecking all of the checkboxes
on the individual treeviews.  Can I do this in Javascript?

Thanks.
0
JeffDrummond
Asked:
JeffDrummond
  • 3
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
Go to browser and copy the complette page html source with both treeviews here as text.

0
 
JeffDrummondAuthor Commented:
The page is quite large.  This is the portion inculding the two treeviews, Regions and Categories.  Thanks!

 <table width="350">
                        <tr>
                            <td valign="top">
                                Regions:&nbsp;</td><td>Categories:</td>
                        </tr>
                        <tr>
                            <td align="left" valign="top">
                                <a href="#ctl00_Body_RegionsTreeview_SkipLink"><img alt="Skip Navigation Links." src="/WebResource.axd?d=-ZmMyGfj3hopVa_HZUpeJg2&amp;t=632883495870637959" width="0" height="0" style="border-width:0px;" /></a><div id="ctl00_Body_RegionsTreeview">
      <table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX2SfMWA3mIjcL_y6D_rPDZQ1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn0CheckBox" id="ctl00_Body_RegionsTreeviewn0CheckBox" checked="checked" title="Asia" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sAsia')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt0');" id="ctl00_Body_RegionsTreeviewt0">Asia</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn1CheckBox" id="ctl00_Body_RegionsTreeviewn1CheckBox" title="Africa" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sAfrica')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt1');" id="ctl00_Body_RegionsTreeviewt1">Africa</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn2CheckBox" id="ctl00_Body_RegionsTreeviewn2CheckBox" checked="checked" title="North America" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sNorth America')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt2');" id="ctl00_Body_RegionsTreeviewt2">North America</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn3CheckBox" id="ctl00_Body_RegionsTreeviewn3CheckBox" title="South America" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sSouth America')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt3');" id="ctl00_Body_RegionsTreeviewt3">South America</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn4CheckBox" id="ctl00_Body_RegionsTreeviewn4CheckBox" title="Europe" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sEurope')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt4');" id="ctl00_Body_RegionsTreeviewt4">Europe</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX4iascjIf74mJB0-PvYVJWk1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_RegionsTreeviewn5CheckBox" id="ctl00_Body_RegionsTreeviewn5CheckBox" title="Australia" /><a class="ctl00_Body_RegionsTreeview_0" href="javascript:__doPostBack('ctl00$Body$RegionsTreeview','sAustralia')" onclick="TreeView_SelectNode(ctl00_Body_RegionsTreeview_Data, this,'ctl00_Body_RegionsTreeviewt5');" id="ctl00_Body_RegionsTreeviewt5">Australia</a></td>
            </tr>
      </table>
</div><a id="ctl00_Body_RegionsTreeview_SkipLink"></a>
                            </td>
                            <td align="left" valign="top">
                                <a href="#ctl00_Body_CategoriesTreeView_SkipLink"><img alt="Skip Navigation Links." src="/WebResource.axd?d=-ZmMyGfj3hopVa_HZUpeJg2&amp;t=632883495870637959" width="0" height="0" style="border-width:0px;" /></a><div id="ctl00_Body_CategoriesTreeView">
      <table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX2SfMWA3mIjcL_y6D_rPDZQ1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn0CheckBox" id="ctl00_Body_CategoriesTreeViewn0CheckBox" title="Prayer" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s1')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt0');" id="ctl00_Body_CategoriesTreeViewt0">Prayer</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><a id="ctl00_Body_CategoriesTreeViewn1" href="javascript:TreeView_ToggleNode(ctl00_Body_CategoriesTreeView_Data,1,ctl00_Body_CategoriesTreeViewn1,'t',ctl00_Body_CategoriesTreeViewn1Nodes)"><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzXwJx33l_NPW57t40m7ht-9kLit0N6NM4XuHy2GC36lIn0&amp;t=632883495870637959" alt="Collapse Volunteer" style="border-width:0;" /></a></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn1CheckBox" id="ctl00_Body_CategoriesTreeViewn1CheckBox" checked="checked" title="Volunteer" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s2')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt1');" id="ctl00_Body_CategoriesTreeViewt1">Volunteer</a></td>
            </tr>
      </table><div id="ctl00_Body_CategoriesTreeViewn1Nodes" style="display:block;">
            <table cellpadding="0" cellspacing="0" style="border-width:0;">
                  <tr>
                        <td><div style="width:20px;height:1px"><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzXwq8Ng0NpL3MLUVHpxHvn5E1&amp;t=632883495870637959" alt="" /></div></td><td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn2CheckBox" id="ctl00_Body_CategoriesTreeViewn2CheckBox" title="Volunteer SubCat 1" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s2\\6')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt2');" id="ctl00_Body_CategoriesTreeViewt2">Volunteer SubCat 1</a></td>
                  </tr>
            </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
                  <tr>
                        <td><div style="width:20px;height:1px"><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzXwq8Ng0NpL3MLUVHpxHvn5E1&amp;t=632883495870637959" alt="" /></div></td><td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX4iascjIf74mJB0-PvYVJWk1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn3CheckBox" id="ctl00_Body_CategoriesTreeViewn3CheckBox" title="Volunteer SubCat 2" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s2\\7')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt3');" id="ctl00_Body_CategoriesTreeViewt3">Volunteer SubCat 2</a></td>
                  </tr>
            </table>
      </div><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn4CheckBox" id="ctl00_Body_CategoriesTreeViewn4CheckBox" title="Advocating" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s3')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt4');" id="ctl00_Body_CategoriesTreeViewt4">Advocating</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX-EvRUG_TP5UCG1aTnSOUWY1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn5CheckBox" id="ctl00_Body_CategoriesTreeViewn5CheckBox" title="Giving" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s4')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt5');" id="ctl00_Body_CategoriesTreeViewt5">Giving</a></td>
            </tr>
      </table><table cellpadding="0" cellspacing="0" style="border-width:0;">
            <tr>
                  <td><img src="/WebResource.axd?d=f8jlEUJvtIzQgBusQxIzX4iascjIf74mJB0-PvYVJWk1&amp;t=632883495870637959" alt="" /></td><td style="white-space:nowrap;"><input type="checkbox" name="ctl00_Body_CategoriesTreeViewn6CheckBox" id="ctl00_Body_CategoriesTreeViewn6CheckBox" title="Serving" /><a class="ctl00_Body_CategoriesTreeView_0" href="javascript:__doPostBack('ctl00$Body$CategoriesTreeView','s5')" onclick="TreeView_SelectNode(ctl00_Body_CategoriesTreeView_Data, this,'ctl00_Body_CategoriesTreeViewt6');" id="ctl00_Body_CategoriesTreeViewt6">Serving</a></td>
            </tr>
      </table>
0
 
ZvonkoSystems architectCommented:
Check this:



<script>
function toggleAll(theBox){
  var theForm = theBox.form;
  var elem = theForm.elements;
  var pref = theBox.name.split("_All")[0];
  for(var i=0;i<elem.length;i++){
    if(elem[i].name.indexOf(pref)==0){
      elem[i].checked = theBox.checked;
    }
  }
}
</script>
<form>
 <table width="350">
                        <tr>
                            <td valign="top">
                                Regions:<br><input type="checkbox" name="ctl00_Body_RegionsTreeview_All" onClick="toggleAll(this)" title="All Regions" /> All</td>
                            <td>Categories:<br><input type="checkbox" name="ctl00_Body_CategoriesTreeView_All" onClick="toggleAll(this)" title="All Categories" /> All</td>
                        </tr>




0
 
JeffDrummondAuthor Commented:
Thanks Zvonko!  Works perfectly!
0
 
ZvonkoSystems architectCommented:
You are welcome.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now