Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Tree Menu

Status
Not open for further replies.

kjohnson530

IS-IT--Management
Mar 26, 2006
28
US
I am new to working with ASPX and would only consider myself to be moderately skilled with ASP. I am currently trying to learn to work with ASP.NET using Visual Web Developer 2005. My question is about building a dynamic tree menu. I have found documentation from Microsoft on how to build the menu (though from what I understand it isn't the optimal way to do it). And though I have the menu the way I would like to see it, I don't know how to link the menu items to produce the effect I would like.

What I'm trying to accomplish is to build a menu on the left, and open specific (selected)data into a new page using a frame on the right.

below is my code. Any help that you all can provide would be greatly appreciated.


Code:
<%@ Page Language="VB" %>
<%@ Import Namespace="System.data" %>
<%@ import Namespace="System.Data.Sqlclient" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<script runat="server">
  
  
    
    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) _
    Handles TreeView1.TreeNodePopulate
        If e.Node.ChildNodes.Count = 0 Then
            Select Case e.Node.Depth
                Case 0
                    PopulateCategories(e.Node)
                Case 1
                    PopulateProducts(e.Node)
            End Select
        End If
    End Sub
  

    Sub PopulateCategories(ByVal node As TreeNode)
        
        Dim Varcat1 As Integer = 0
        Dim sqlQuery As New SqlCommand( _
            "Select Status,StatusID From tblstatus")
        Dim ResultSet As DataSet
        ResultSet = RunQuery(sqlQuery)
        If ResultSet.Tables.Count > 0 Then
            Dim row As DataRow
            For Each row In ResultSet.Tables(0).Rows
                Dim NewNode As TreeNode = New _
                    TreeNode(row("Status").ToString(), _
                    row("StatusID").ToString())
                NewNode.PopulateOnDemand = True
                NewNode.SelectAction = TreeNodeSelectAction.Expand
                node.ChildNodes.Add(NewNode)
            Next
        End If
    End Sub
   
    Sub PopulateProducts(ByVal node As TreeNode)
        Dim sqlQuery As New SqlCommand
        sqlQuery.CommandText = "Select WOID From tblWO " & _
            " Where StatusID = @StatusID "
        sqlQuery.Parameters.Add("Statusid", SqlDbType.Int).Value = _
            node.Value
        Dim ResultSet As DataSet = RunQuery(sqlQuery)
        If ResultSet.Tables.Count > 0 Then
            Dim row As DataRow
            For Each row In ResultSet.Tables(0).Rows
                Dim NewNode As TreeNode = New _
                    TreeNode(row("WOId").ToString())
                NewNode.PopulateOnDemand = False
                NewNode.SelectAction = TreeNodeSelectAction.None
                node.ChildNodes.Add(NewNode)
            Next
        End If
    End Sub

    Function RunQuery(ByVal sqlQuery As SqlCommand) As DataSet
        Dim connectionString As String
        connectionString = _
            ConfigurationManager.ConnectionStrings _
            ("ITConnectionString").ConnectionString
        Dim dbConnection As New SqlConnection
        dbConnection.ConnectionString = connectionString
        Dim dbAdapter As New SqlDataAdapter
        dbAdapter.SelectCommand = sqlQuery
        sqlQuery.Connection = dbConnection
        Dim resultsDataSet As DataSet = New DataSet
        Try
            dbAdapter.Fill(resultsDataSet)
        Catch ex As Exception
            labelStatus.Text = "Unable to connect to SQL Server."
        End Try
        Return resultsDataSet
    End Function
    
</script>

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" MaxDataBindDepth="2" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
            Style="z-index: 100; left: 20px; position: absolute; top: 94px" ExpandDepth="1" Height="28px" Width="74px">
            <Nodes>
                <asp:TreeNode PopulateOnDemand="True" Text="Status" Value="Product List" NavigateUrl="~/WO/WOReview.aspx" Target="Main"></asp:TreeNode>
            </Nodes>
        </asp:TreeView>
        <asp:Label ID="LabelStatus" runat="server" Style="z-index: 102; left: 12px; position: absolute;
            top: 66px" Text="Label" Width="131px" Visible="False"></asp:Label>
    
    </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ITConnectionString %>"
            SelectCommand="SELECT * FROM [tblWO]"></asp:SqlDataSource>
    </form>
</body>
</html>


Thanks in advance for helping a novice out.
 
The code that I have presented populates my tree menu. I adjusted this code from an example provided on Microsoft's website. My problem is that I want to do more than populate menu. I would like each of the entries to link to a form page which will appear in a regular frame on my page. I have since modified the subroutine -- Populate Products with the following code:



Code:
Dim NewNode as TreeNode = New _
TreeNode(row("WOId").Tostring()

NewNode.NavigateUrl ="[URL unfurl="true"]http://www.espn.com"[/URL]
NewNode.Target = "Main"
NewNode.PopulateOnDemand = True
'NewNode.SelectAction = TreeNodeSelectAction.None
node.ChildNodes.Add(NewNode)

This of course works fine if I want every single one of my links to go to the same page. Which I do not. What I would like to see is a link to:


Code:
NavigateURL="~/WO/Test.aspx?txtsearch=100"


I would like the 100 to be replaced by my value that is being added as the tree menu value.
 
I wonder if I can give myself props for answering my own question.

I modified my code to appear as follows:

Code:
Dim NewNode as TreeNode = New _
TreeNode(row("WOId").Tostring()

NewNode.NavigateUrl ="~/WO/WOReview.aspx?txtsearch=" & (row("WOId").Tostring())
NewNode.Target = "Main"
NewNode.PopulateOnDemand = True
'NewNode.SelectAction = TreeNodeSelectAction.None
node.ChildNodes.Add(NewNode)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top