Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Treeview, loosing selected node after navigate, need help from     experts here :)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Falcula - 04 Dec 2007 09:49 GMT
Hello,

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;
}
<%=javascript%>

</script>
<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"
HorizontalPadding="0px"
       VerticalPadding="0px" />
   <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="2px"
       NodeSpacing="0px" VerticalPadding="2px" />
       <Nodes>
             <asp:TreeNode Text="Root" SelectAction="Select"
               PopulateOnDemand="True" Value="0"/>
        </Nodes>
</asp:TreeView>

       protected void PopulateNode(Object sender, TreeNodeEventArgs
e)
       {

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

       }

       private void PopulateTopLevelNodes(TreeNode node)
       {

           Selago.BusinessLayer.Service service = new
Selago.BusinessLayer.Service();
           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 =
TreeNodeSelectAction.SelectExpand;
                       node.ChildNodes.Add(TopLevel);

                   }
               }
       }

       private void PopulateChildern(TreeNode node)
       {

           Selago.BusinessLayer.Service service = new
Selago.BusinessLayer.Service();
           ChildrenList =
service.GetChildrenPages(Int32.Parse(node.Value));

           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 =
TreeNodeSelectAction.SelectExpand;

               // Add the new node to the ChildNodes collection of
the parent node.
               node.ChildNodes.Add(newNode);

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

               if (ChildrensSubCount > 0)
               {
                   PopulateChildern(newNode);
               }

           }
       }

       protected void nodeSelect(object sender, EventArgs e)
       {
           javascript = "OnPageClick(" +
SubMenuTree.SelectedNode.Value + ");";
       }
Scott Roberts - 04 Dec 2007 16:27 GMT
> Hello,
>
> 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
"lost".

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).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.