943,907 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 11267
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2009
0

Check/uncheck checkboxes in TreeView using javascript

Expand Post »
Hi,

In my ASP.NET page I have a TreeView where ShowCheckBoxes="Leaf".
I want to mark as checked/unchecked all the visible checkboxes by clicking in a button.

My code for selecting all/none nodes is like this:

javascript Syntax (Toggle Plain Text)
  1. function selectAllNone(tvID, value) {
  2. var tvNodes = document.getElementById(tvID);
  3. var chBoxes = tvNodes.getElementByTagName("input");
  4. for (var i = 0; i < chBoxes.length; i++) {
  5. var chk = chkBox[i];
  6. if (chk.type == "checkbox") {
  7. chk.cheked = value;
  8. }
  9. }
  10. }

However, in the line var chBoxes = tvNodes.getElementByTagName("input");I'm having this error message: object doesn't support this property or method.
How do I checj/uncheck all the checkboxes, then?

Thanks,

Ana
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ana D. is offline Offline
76 posts
since Jul 2009
Sep 17th, 2009
1

Re: Check/uncheck checkboxes in TreeView using javascript

i think that method is not getElementByTagName, but getElementsByTagName, so it is plural basically. try that way.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 17th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

here you go :

<script language="javascript" type="text/javascript">
// <!CDATA[

ASP.NET Syntax (Toggle Plain Text)
  1. function Button1_onclick() {
  2. var inputs = document.forms[0].elements;
  3. for(var i=0;i< inputs.length;i++)
  4. {
  5. inputs[i].checked = true;
  6. }
  7. }
  8.  
  9. function Button2_onclick() {
  10. var inputs = document.forms[0].elements;
  11. for(var i=0;i< inputs.length;i++)
  12. {
  13. inputs[i].checked = false;
  14. }
  15. }
  16.  
  17. // ]]>
  18. </script>
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 17th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

working sample is attached to this post.
Attached Files
File Type: zip WebSite2.zip (1.9 KB, 451 views)
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 17th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

Just in case you cant run the attachment :
Default.aspx :
ASP.NET Syntax (Toggle Plain Text)
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. <script language="javascript" type="text/javascript">
  9. // <!CDATA[
  10.  
  11. function Button1_onclick() {
  12. var inputs = document.forms[0].elements;
  13. for(var i=0;i< inputs.length;i++)
  14. {
  15. inputs[i].checked = true;
  16. }
  17. }
  18.  
  19. function Button2_onclick() {
  20. var inputs = document.forms[0].elements;
  21. for(var i=0;i< inputs.length;i++)
  22. {
  23. inputs[i].checked = false;
  24. }
  25. }
  26.  
  27. // ]]>
  28. </script>
  29. </head>
  30. <body>
  31. <form id="form1" runat="server">
  32. <div>
  33. &nbsp;<input id="Button1" type="button" value="Select All" onclick="Button1_onclick()" />
  34. <input id="Button2" type="button" value="Deselect All" onclick="return Button2_onclick()" /><br />
  35. <br />
  36. <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All">
  37. <Nodes>
  38. <asp:TreeNode Text="New Node" Value="New Node">
  39. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  40. </asp:TreeNode>
  41. <asp:TreeNode Text="New Node" Value="New Node">
  42. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  43. </asp:TreeNode>
  44. <asp:TreeNode Text="New Node" Value="New Node">
  45. <asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
  46. </asp:TreeNode>
  47. </Nodes>
  48. </asp:TreeView>
  49. &nbsp;</div>
  50. </form>
  51. </body>
  52. </html>
Last edited by serkan sendur; Sep 17th, 2009 at 8:36 pm.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 18th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

Hi Serkan,

You were right: the method is getElementsByTagName. I just changed it and it's working.

Thanks =)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ana D. is offline Offline
76 posts
since Jul 2009
Sep 18th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

Be generous to add to my reputation
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 18th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

I tried to, but how can I do it?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ana D. is offline Offline
76 posts
since Jul 2009
Sep 18th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

in the beginning of each post there is a "add to serkan's reputation" link , you basically click on it and then select agree radio button then type something then click ok.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 18th, 2009
0

Re: Check/uncheck checkboxes in TreeView using javascript

Added =)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Ana D. is offline Offline
76 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Multiple form error in ASP.net
Next Thread in ASP.NET Forum Timeline: Specified argument was out of the range of valid values. Parameter name: value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC