emsd123 0 Newbie Poster

Hello, everyone.
I have generated a treeview loading from database, learned from a book.
But I have no idea how can I save the view state of my treeview,
I have tried the following code to do,
through it, I can only keep expanding the first layer of treeview,
but when I try to expand the second layer, it will collapse all layers again.

RootNode
|
|
  --- first layer
    |
     ---- second layer
subNode.CollapseAll()

If (subNode.Selected = True) Then

subNode.Expand()

End If

so please help me figure out how can I do this.

Here is my code to generate the treeview:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim ckint As Integer = 0


            If Request("al_sid") IsNot Nothing Then
                If Integer.TryParse(Request("al_sid"), ckint) Then
                    get_id.Text = ckint.ToString()
                Else
                    get_id.Text = "0"
                End If
            End If

            BuildTreeView()
        End If
    End Sub


    Private Sub BuildTreeView()
        Dim RootNode As New TreeNode()
        RootNode.Text = "VED"
        RootNode.Value = "0"
        RootNode.NavigateUrl = "3001.aspx?al_sid=0"
        RootNode.Target = "_parent"
        RootNode.ImageUrl = "../TreeLineImages/home-icon6.gif"

        If get_id.Text = "0" Then
            RootNode.Select()
        End If

        treeview1.Nodes.Clear()
        treeview1.Nodes.Add(RootNode)

        Using Sql_Conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("AppSysConnectionString").ConnectionString)
            Sql_Conn.Open()

            Dim SqlString As String = ""

            SqlString = "Select al_sid, up_al_sid, al_name, type, filepath  From Al_List Order by up_al_sid, type, al_sort"

            Using Sql_Command As New SqlCommand(SqlString, Sql_Conn)
                Using Sql_Adapter As New SqlDataAdapter(Sql_Command)
                    Dim dt_treeview1 As New DataTable()

                    Sql_Adapter.Fill(dt_treeview1)

                    AddNodes(RootNode, dt_treeview1, 0)

                    dt_treeview1.Clear()
                    dt_treeview1.Dispose()

                End Using
            End Using
            Sql_Conn.Close()
        End Using

    End Sub

    Private Sub AddNodes(ByRef pNode As TreeNode, ByRef dt_treeview1 As DataTable, ByVal up_al_sid As Integer)
        Dim dRow As DataRow() = dt_treeview1.Select("up_al_sid = " & up_al_sid.ToString())


        If dRow.GetUpperBound(0) > -1 Then
            Dim subNode As TreeNode

            For Each sRow As DataRow In dRow
                subNode = New TreeNode()

                If sRow(0).ToString() = get_id.Text Then
                    subNode.Select()
                End If

                subNode.Text = sRow(2).ToString()
                subNode.Value = sRow(0).ToString()


                If sRow(3).ToString() = "1" Then
                    subNode.NavigateUrl = "tenderprofile.aspx?al_sid=" & sRow(0).ToString()
                    subNode.Target = "_parent"
                    subNode.ImageUrl = "../TreeLineImages/edit-page-blue.gif"

                ElseIf sRow(3).ToString() = "2" Then
                    subNode.NavigateUrl = "documents/" & sRow(4).ToString()
                    subNode.Target = "_blank"
                    subNode.ImageUrl = "../TreeLineImages/down-blue.gif"

                Else
                    subNode.NavigateUrl = "3001.aspx?al_sid=" & sRow(0).ToString()
                    subNode.Target = "_parent"

                    subNode.CollapseAll()
                    If (subNode.Selected = True) Then
                        subNode.Expand()
                    End If

                    If (subNode.Expanded = False) Then
                        subNode.ImageUrl = "../TreeLineImages/folder-blue-fold.gif"
                    Else
                        subNode.ImageUrl = "../TreeLineImages/folder-blue.gif"
                        subNode.Expand()
                    End If
                End If

                pNode.ChildNodes.Add(subNode)

                AddNodes(subNode, dt_treeview1, Integer.Parse(sRow(0).ToString()))
            Next
            dRow = Nothing
        End If
    End Sub