Click here to Skip to main content
15,891,375 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Questionimportant help to me and others Pin
appula14-Nov-07 12:24
appula14-Nov-07 12:24 
AnswerRe: important help to me and others Pin
Not Active14-Nov-07 12:39
mentorNot Active14-Nov-07 12:39 
GeneralRe: important help to me and others Pin
appula15-Nov-07 8:43
appula15-Nov-07 8:43 
QuestionDynamic menu Pin
Matt Cavanagh14-Nov-07 11:56
Matt Cavanagh14-Nov-07 11:56 
AnswerRe: Dynamic menu Pin
Christian Graus14-Nov-07 12:06
protectorChristian Graus14-Nov-07 12:06 
AnswerRe: Dynamic menu Pin
Not Active14-Nov-07 12:43
mentorNot Active14-Nov-07 12:43 
AnswerRe: Dynamic menu Pin
Expert Coming14-Nov-07 13:39
Expert Coming14-Nov-07 13:39 
Questionmaintaining state of treeview and doing postback Pin
uglyeyes14-Nov-07 11:40
uglyeyes14-Nov-07 11:40 
Hi! I couldnt maintain the state of the treeview after postback. searched all over but i could seem to solve the problem. below is the code that i am using so it will be easier to understand whta i am doing. also i have having problem navigating to the same url with parameter on it. for e.g. if the node id is 5 i want to postback as tree.aspx?id=5


Imports System.Data.SqlClient
Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI.WebControls

Partial Public Class tree
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
PopulateRootLevel()

End If
'TreeView1.CollapseAll()
' get the saved state of all nodes.
Dim treeViewState As New TreeViewState()
treeViewState.SaveTreeView(TreeView1, Me.GetType.ToString())

End Sub
Private Sub PopulateRootLevel()
Dim objConn As New SqlConnection("server=APEX-KEV3;uid=sa;pwd=password2133;database=TaxnologyOnlineDev")
Dim objCommand As New SqlCommand("select LocationId,LocationName,(select count(*) FROM Location " _
& "WHERE parentlocationid=sc.locationid) childnodecount FROM Location sc where parentlocationID is null and locationid=3", _
objConn)
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
PopulateNodes(dt, TreeView1.Nodes)
End Sub
Private Sub PopulateNodes(ByVal dt As DataTable, ByVal nodes As TreeNodeCollection)
For Each dr As DataRow In dt.Rows
Dim tn As New TreeNode()
tn.Text = dr("locationname").ToString()
tn.Value = dr("locationid").ToString()
nodes.Add(tn)

'If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal parentNode As TreeNode)

Dim objConn As New SqlConnection("server=APEX-KEV3;uid=sa;pwd=password2133;database=TaxnologyOnlineDev")
Dim objCommand As New SqlCommand("select LocationId,LocationName,(select count(*) FROM Location " _
& "WHERE parentlocationid=sc.locationid) childnodecount FROM Location sc where parentlocationID=@parentID", _
objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid

Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
PopulateNodes(dt, parentNode.ChildNodes)

End Sub
Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.SelectedNodeChanged
If TreeView1.SelectedNode.Value <> String.Empty Then
Response.Redirect("tree.aspx?id=" + TreeView1.SelectedNode.Value)
End If
End Sub
Protected Sub TreeView1_Unload(ByVal sender As Object, ByVal e As EventArgs) Handles TreeView1.Unload
' save the state of all nodes.
Dim tvs As New TreeViewState
tvs.SaveTreeView(TreeView1, Me.GetType().ToString())
End Sub
End Class

Public Class TreeViewState
Public Sub SaveTreeView(ByVal treeView As TreeView, ByVal key As String)
Dim list As New List(Of System.Nullable(Of Boolean))()
SaveTreeViewExpandedState(treeView.Nodes, list)
HttpContext.Current.Session(key + treeView.ID) = list
End Sub
Private RestoreTreeViewIndex As Integer
Public Sub RestoreTreeView(ByVal treeView As TreeView, ByVal key As String)
Dim list As New List(Of Nullable(Of Boolean))
If HttpContext.Current.Session(key + treeView.ID) IsNot Nothing Then
list = CType(HttpContext.Current.Session(key + treeView.ID), List(Of Nullable(Of Boolean)))
End If
RestoreTreeViewIndex = 0
RestoreTreeViewExpandedState(treeView.Nodes, list)

End Sub

Private Sub SaveTreeViewExpandedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of System.Nullable(Of Boolean)))
For Each node As TreeNode In nodes
list.Add(node.Expanded)
If node.ChildNodes.Count > 0 Then
SaveTreeViewExpandedState(node.ChildNodes, list)
End If
Next
End Sub
Private Sub RestoreTreeViewExpandedState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of System.Nullable(Of Boolean)))
For Each node As TreeNode In nodes
If RestoreTreeViewIndex >= list.Count Then
Exit For
End If
node.Expanded = list(System.Math.Max(System.Threading.Interlocked.Increment(RestoreTreeViewIndex), RestoreTreeViewIndex - 1))
If node.ChildNodes.Count > 0 Then
RestoreTreeViewExpandedState(node.ChildNodes, list)
End If
Next
End Sub
End Class

i am using visual studio .net 2.0 treeview control
please help

thanks

AnswerRe: maintaining state of treeview and doing postback Pin
Not Active14-Nov-07 12:47
mentorNot Active14-Nov-07 12:47 
GeneralRe: maintaining state of treeview and doing postback [modified] Pin
uglyeyes14-Nov-07 13:38
uglyeyes14-Nov-07 13:38 
GeneralRe: maintaining state of treeview and doing postback Pin
Not Active14-Nov-07 15:28
mentorNot Active14-Nov-07 15:28 
GeneralRe: maintaining state of treeview and doing postback Pin
uglyeyes14-Nov-07 16:56
uglyeyes14-Nov-07 16:56 
GeneralRe: maintaining state of treeview and doing postback Pin
Not Active14-Nov-07 17:24
mentorNot Active14-Nov-07 17:24 
GeneralRe: maintaining state of treeview and doing postback Pin
uglyeyes15-Nov-07 14:40
uglyeyes15-Nov-07 14:40 
GeneralRe: maintaining state of treeview and doing postback Pin
uglyeyes15-Nov-07 17:22
uglyeyes15-Nov-07 17:22 
QuestionComples Parameters in URL Pin
freshonlineMax14-Nov-07 9:08
freshonlineMax14-Nov-07 9:08 
AnswerDouble post Pin
pmarfleet14-Nov-07 9:33
pmarfleet14-Nov-07 9:33 
AnswerRe: Comples Parameters in URL Pin
Guffa14-Nov-07 9:34
Guffa14-Nov-07 9:34 
AnswerRe: Using Complex Parameters in URL Pin
pmarfleet14-Nov-07 9:35
pmarfleet14-Nov-07 9:35 
QuestionOutput problem Pin
McCollough14-Nov-07 8:56
McCollough14-Nov-07 8:56 
AnswerRe: Output problem Pin
Christian Graus14-Nov-07 12:00
protectorChristian Graus14-Nov-07 12:00 
QuestionRe: Output problem Pin
McCollough15-Nov-07 2:46
McCollough15-Nov-07 2:46 
QuestionHow to create the custom control in asp.net 2.0 Pin
Murugan.nett14-Nov-07 8:53
Murugan.nett14-Nov-07 8:53 
AnswerRe: How to create the custom control in asp.net 2.0 Pin
pmarfleet14-Nov-07 9:54
pmarfleet14-Nov-07 9:54 
GeneralRe: How to create the custom control in asp.net 2.0 Pin
Murugan.nett15-Nov-07 3:32
Murugan.nett15-Nov-07 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.