469,645 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Treeview, loosing selected node after navigate, need help from


I have a treeview control, when i select a item i navigate to url. But
selected node is lost, it reset itself, loosing state.

I post my code here. Thanks in advance.

<script language="javascript">
var selectedPageId = 1;
function OnPageClick(pageId)
selectedPageId = pageId;
parent.location = "default.aspx?id="+pageId;

<asp:TreeView ID="SubMenuTree" EnableClientScript="true"
EnableViewState="true" ExpandDepth="1"
OnSelectedNodeChanged="nodeSelect" OnTreeNodePopulate="PopulateNode"
runat="server" ImageSet="XPFileExplorer" NodeIndent="15">
<ParentNodeStyle Font-Bold="False" />
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
VerticalPadding="0px" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
NodeSpacing="0px" VerticalPadding="2px" />
<asp:TreeNode Text="Root" SelectAction="Select"
PopulateOnDemand="True" Value="0"/>

protected void PopulateNode(Object sender, TreeNodeEventArgs

// Call the appropriate method to populate a node at a
particular level.
switch (e.Node.Depth)
case 0:
// Populate the first-level nodes.
case 1:
// Populate the second-level nodes.
// Do nothing.

private void PopulateTopLevelNodes(TreeNode node)

Selago.BusinessLayer.Service service = new
PageList = service.GetPages();

foreach(iPage page in PageList)
if(page.ParentID == 0)
TreeNode TopLevel = new TreeNode();

TopLevel.Text = page.PageName;
TopLevel.Value = page.ID.ToString();

// Add root node to TreeView
TopLevel.PopulateOnDemand = true;
TopLevel.SelectAction =


private void PopulateChildern(TreeNode node)

Selago.BusinessLayer.Service service = new
ChildrenList =

foreach (iPage page in ChildrenList)
TreeNode newNode = new TreeNode();

newNode.Text = page.PageName;
newNode.Value = page.ID.ToString();

// Set additional properties for the node.
newNode.SelectAction =

// Add the new node to the ChildNodes collection of
the parent node.

DataSet ChildrensSub = new DataSet();
int ChildrensSubCount =
service.GetChildrenPages(Int32.Parse(newNode.Value )).Count;

if (ChildrensSubCount 0)

protected void nodeSelect(object sender, EventArgs e)
javascript = "OnPageClick(" +
SubMenuTree.SelectedNode.Value + ");";

Dec 4 '07 #1
1 3281

"Falcula" <je************@yahoo.sewrote in message
news:5a**********************************@v4g2000h sf.googlegroups.com...

I have a treeview control, when i select a item i navigate to url. But
selected node is lost, it reset itself, loosing state.

The selected node is stored in the viewstate. Viewstate is only used on
postbacks, not redirects. So when you redirect the user to a new page, the
viewstate is not used on the new page and that's why the selected node is

If you want to "remember" the selected node you'll need to pass it in the
query string, post data, or keep it on the server somehow (e.g. session).

Dec 4 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Steve | last post: by
4 posts views Thread by Ian Powell | last post: by
6 posts views Thread by L.M | last post: by
3 posts views Thread by Shawn | last post: by
2 posts views Thread by Claus | last post: by
8 posts views Thread by Matt MacDonald | last post: by
18 posts views Thread by =?Utf-8?B?TGkgV2VuZw==?= | last post: by
2 posts views Thread by govolsbaby | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.