tcadieux
06-13-2008, 06:08 PM
The code below builds a Treeview from my database, then enumerates through the nodes and sets the Checkbox checked if my user has those picked in their profile.
2 issues, firstly, this code runs ok on the page where the user picks what they want, but on my Admin page, i don't want the Admin to have to click on each node to see what the user picked, i'd like the Tree to open fully expanded, I can't do that with the original way I set this up, using ExpandOnDemand. I tried removing ExpandonDemand and setting e as TreeNodeEventArgs in the Page_Load even but it gives me a CAST error.
Also, I'm not sure that this is the most efficient way to ckeck what was picked by the user?
<p><asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue"
EnableClientScript="true"
PopulateNodesFromClient="true" OnTreeNodePopulate="PopulateNode"
ShowCheckBoxes="Leaf" ShowExpandCollapse="true"
runat="server">
<Nodes>
<asp:TreeNode Text="CNAD Mailer Groups" SelectAction="Expand" PopulateOnDemand="true" />
</Nodes>
</asp:TreeView>
Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Call the appropriate method to populate a node at a particular level.
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateGroups(e.Node)
Case 1
' Populate the second-level nodes.
PopulateLists(e.Node)
Case Else
' Do nothing.
End Select
End Sub
Sub PopulateGroups(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("Select * from tblGroups")
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim newNode As TreeNode = New TreeNode()
newNode.Text = row("Groups_Name").ToString()
newNode.Value = row("Groups_ID").ToString()
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
newNode.PopulateOnDemand = True
' Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode)
Next
End If
End Sub
Sub PopulateLists(ByVal node As TreeNode)
' Query for the products of the current category. These are the values
' for the third-level nodes.
Dim ResultSet As DataSet = RunQuery("Select * From tblDistList Where groups_id=" & node.Value)
' Create the third-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node.
'Dim NewNode As TreeNode = New TreeNode(row("DistList_Name").ToString())
Dim NewNode As TreeNode = New TreeNode
NewNode.Text = (row("DistList_Name").ToString())
NewNode.Value = (row("DistList_ID"))
' Set the PopulateOnDemand property to false, because these are leaf nodes and
' do not need to be populated.
NewNode.PopulateOnDemand = False
' Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.None
objConnection = New SqlConnection(sConn)
strSQL = strSQL & "SELECT group_id, distlist_id "
strSQL = strSQL & " FROM tblnames_distLists "
strSQL = strSQL & " WHERE names_id=" & Request.QueryString("id")
objCommand = New SqlCommand(strSQL, objConnection)
objConnection.Open()
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
Do While objDataReader.Read
If Int32.Parse(objDataReader("distlist_id")) = Int32.Parse(NewNode.Value) _
And Int32.Parse(objDataReader("group_id")) = Int32.Parse(node.Value) Then
NewNode.Checked = True
End If
Loop
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
objConnection.Close()
Next
End If
End Sub
Function RunQuery(ByVal QueryString As String) As DataSet
Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ConnectionString
Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
Dim DBAdapter As SqlDataAdapter
Dim ResultsDataSet As DataSet = New DataSet
Dim message As New Label
Try
' Run the query and create a DataSet.
DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
DBAdapter.Fill(ResultsDataSet)
' Close the database connection.
DBConnection.Close()
Catch ex As Exception
' Close the database connection if it is still open.
If DBConnection.State = ConnectionState.Open Then
DBConnection.Close()
End If
message.Text = "Unable to connect to the database."
End Try
Return ResultsDataSet
End Function
2 issues, firstly, this code runs ok on the page where the user picks what they want, but on my Admin page, i don't want the Admin to have to click on each node to see what the user picked, i'd like the Tree to open fully expanded, I can't do that with the original way I set this up, using ExpandOnDemand. I tried removing ExpandonDemand and setting e as TreeNodeEventArgs in the Page_Load even but it gives me a CAST error.
Also, I'm not sure that this is the most efficient way to ckeck what was picked by the user?
<p><asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue"
EnableClientScript="true"
PopulateNodesFromClient="true" OnTreeNodePopulate="PopulateNode"
ShowCheckBoxes="Leaf" ShowExpandCollapse="true"
runat="server">
<Nodes>
<asp:TreeNode Text="CNAD Mailer Groups" SelectAction="Expand" PopulateOnDemand="true" />
</Nodes>
</asp:TreeView>
Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Call the appropriate method to populate a node at a particular level.
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateGroups(e.Node)
Case 1
' Populate the second-level nodes.
PopulateLists(e.Node)
Case Else
' Do nothing.
End Select
End Sub
Sub PopulateGroups(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("Select * from tblGroups")
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim newNode As TreeNode = New TreeNode()
newNode.Text = row("Groups_Name").ToString()
newNode.Value = row("Groups_ID").ToString()
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
newNode.PopulateOnDemand = True
' Set additional properties for the node.
newNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(newNode)
Next
End If
End Sub
Sub PopulateLists(ByVal node As TreeNode)
' Query for the products of the current category. These are the values
' for the third-level nodes.
Dim ResultSet As DataSet = RunQuery("Select * From tblDistList Where groups_id=" & node.Value)
' Create the third-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node.
'Dim NewNode As TreeNode = New TreeNode(row("DistList_Name").ToString())
Dim NewNode As TreeNode = New TreeNode
NewNode.Text = (row("DistList_Name").ToString())
NewNode.Value = (row("DistList_ID"))
' Set the PopulateOnDemand property to false, because these are leaf nodes and
' do not need to be populated.
NewNode.PopulateOnDemand = False
' Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.None
objConnection = New SqlConnection(sConn)
strSQL = strSQL & "SELECT group_id, distlist_id "
strSQL = strSQL & " FROM tblnames_distLists "
strSQL = strSQL & " WHERE names_id=" & Request.QueryString("id")
objCommand = New SqlCommand(strSQL, objConnection)
objConnection.Open()
objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
Do While objDataReader.Read
If Int32.Parse(objDataReader("distlist_id")) = Int32.Parse(NewNode.Value) _
And Int32.Parse(objDataReader("group_id")) = Int32.Parse(node.Value) Then
NewNode.Checked = True
End If
Loop
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
objConnection.Close()
Next
End If
End Sub
Function RunQuery(ByVal QueryString As String) As DataSet
Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ConnectionString
Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
Dim DBAdapter As SqlDataAdapter
Dim ResultsDataSet As DataSet = New DataSet
Dim message As New Label
Try
' Run the query and create a DataSet.
DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
DBAdapter.Fill(ResultsDataSet)
' Close the database connection.
DBConnection.Close()
Catch ex As Exception
' Close the database connection if it is still open.
If DBConnection.State = ConnectionState.Open Then
DBConnection.Close()
End If
message.Text = "Unable to connect to the database."
End Try
Return ResultsDataSet
End Function