Treeview and checkboxes problem

Z

Zarko Ursic

Hi,

I try to set programmatically treeview checkboxes. Basically the user choses
a configuration from a dropdownlist that triggers a database query returning
the required IDs of the treeview elements that have to be checked.
The following code is triggered when an item is chosen from a dropdownlist:

(dr is the returned datareader containing "ID" and "label", the treeview is
3 levels deep)

Dim i, j, k As Integer
If dr.HasRows Then
Do While dr.Read()
For i = 0 To tvFilters.Nodes.Count - 1
For j = 0 To tvFilters.Nodes(i).Nodes.Count - 1
If tvFilters.Nodes(i).Nodes.Item(j).ID = dr.GetInt32(0) Then
tvFilters.Nodes(i).Nodes.Item(j).Checked = True
Else
tvFilters.Nodes(i).Nodes.Item(j).Checked = False
End If
For k = 0 To tvFilters.Nodes(i).Nodes.Item(j).Nodes.Count - 1
If tvFilters.Nodes(i).Nodes.Item(j).Nodes.Item(k).ID =
dr.GetInt32(0) Then
tvFilters.Nodes(i).Nodes.Item(j).Nodes.Item(k).Checked =
True
Else
tvFilters.Nodes(i).Nodes.Item(j).Nodes.Item(k).Checked =
False
End If
Next
Next
Next
Loop
End If

All needed values go correctly through the test but when the test is
successfull, the previous checked item is automatically set to False! Only
the last value remains True and my tree has only one item checked!
Which kind of event can modify my checked value during the execution of this
piece of code?
Thanks in advance,
Zarko
 
J

Jeffrey Tan[MSFT]

Hi Zarko,

Thank you for posting in the community!

Based on my understanding, all your treeview control's items have checkbox.
You use DropDownList control to fire event and retrieve data from the
database. Then checked the checkbox through the data. But at last, only one
item is checked.(Last Item)

=========================================
In your code, I did not see anything incorrect. I think you should paste
all the code snippet to reproduce your problem.

I do the test project like this:
<asp:DropDownList id="DropDownList1" runat="server" Width="160px"
Height="40px" AutoPostBack="True">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
<iewc:TreeView id="TreeView1" runat="server" Width="240px" Height="264px">
<iewc:TreeNode NodeData="1" CheckBox="True" Text="Node0">
<iewc:TreeNode NodeData="3" CheckBox="True" Text="Node0">
<iewc:TreeNode NodeData="2" CheckBox="True" Text="Node2"></iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode NodeData="3" CheckBox="True" Text="Node1"></iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode NodeData="1" CheckBox="True" Text="Node1">
<iewc:TreeNode NodeData="2" CheckBox="True" Text="Node3">
<iewc:TreeNode NodeData="3" CheckBox="True"
Text="Node4"></iewc:TreeNode>
</iewc:TreeNode>
</iewc:TreeNode>
<iewc:TreeNode NodeData="2" CheckBox="True" Text="Node2">
<iewc:TreeNode NodeData="3" CheckBox="True" Text="Node5">
<iewc:TreeNode NodeData="2" CheckBox="True" Text="Node3"></iewc:TreeNode>
</iewc:TreeNode>
</iewc:TreeNode>
</iewc:TreeView>

Then, in code behind:
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim val As String = DropDownList1.SelectedValue
Dim i, j, k As Integer
For i = 0 To TreeView1.Nodes.Count - 1
For j = 0 To TreeView1.Nodes(i).Nodes.Count - 1
If TreeView1.Nodes(i).Nodes.Item(j).NodeData = val Then
TreeView1.Nodes(i).Nodes.Item(j).Checked = True
Else
TreeView1.Nodes(i).Nodes.Item(j).Checked = False
End If
For k = 0 To TreeView1.Nodes(i).Nodes.Item(j).Nodes.Count -
1
If
TreeView1.Nodes(i).Nodes.Item(j).Nodes.Item(k).NodeData = val Then

TreeView1.Nodes(i).Nodes.Item(j).Nodes.Item(k).Checked = True
Else

TreeView1.Nodes(i).Nodes.Item(j).Nodes.Item(k).Checked = False
End If
Next
Next
Next
End Sub

In the code, I check all the nodes whoes NodeData are the same as the
selected value in dropdownlist.(Except the first level nodes)

In your code, I modified ID property to NodeData property, because in a web
page, the ID property must be unique. (Is this your problem?)

=======================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Z

Zarko Ursic

Hi Jeffrey,
thank you for your extremely quick reply. I spotted it: the problem is
not the treeview, it is my loop within datareader. For each loop I check
the right value and uncheck all the rest. As for each loop the values
are different, only the last one is checked and all the rest unchecked.

I corrected it by clearing the treeview at the beginning and then
checking the found values - that is.

Thank you very much nonetheless: as your example obviously works, that
helped me to look elsewhere.

Zarko
 
J

Jeffrey Tan[MSFT]

Hi Zarko,

Also thanks very much for your quick feedback. :)

I am glad you have found out what you want.

If you have any further question, please feel free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top