SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TreeView - Adding Node After A DataBind

    I have a treeview that has a datasource of an XML SiteMap file.

    Code HTML4Strict:
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" SkinID="SiteMap" ImageSet="Custom">
       <ParentNodeStyle Font-Bold="false" />
       <HoverNodeStyle Font-Underline="True" />   
       <SelectedNodeStyle Font-Underline="True" HorizontalPadding="5px" VerticalPadding="0px" />
       <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
    </asp:TreeView>

    Is there a way in codebehind or somwhere else to add a node without modifying the XML file?

    I have a theme system set up. All themes use the same sitemap, but for one node, i need to have a dynamic navigateurl value.

    so if i could databind the treeview and then add a node via codebehind that would do the trick, but i don't see how i can do that.

    any ideas?

    Ras.

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    A better angle to hit this from is the data model. Meaning write a custom SiteMapProvider that provides the dynamic URL.

    You could also catch the OnItemDataBound event from the tree view and do some sanity checking, but that doesn't scale so well.

  3. #3
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't find any ItemDataBound property for TreeView.

    Could you give me a bit of a kick start with the provider? I am not sure where to begin.

    my web.sitemap file Looks like this:

    Code XML:
     
    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
      <siteMapNode title="Home" description="Home" url="~/default.aspx">
     
        <siteMapNode title="Participants" description="Add/Edit Participants and Entries" url="~/subadmin" roles="SubAdmin" />
        <siteMapNode title="Notes" description="Add/Edit/Delete Notes" url="~/subadmin/notes.aspx" roles="SubAdmin" />
     
        <siteMapNode title="My Profile" description="" url="~/members/myProfile.aspx" roles="*" />
        <siteMapNode title="How To Play" description="How To Play" url="~/members/viewStory.aspx?SID=27" roles="members" />
        <siteMapNode title="Change Password" description="" url="~/members/newPassword.aspx" roles="*" />
        <siteMapNode title="My Selections" description="My Major Tournament Selections" url="~/members/mySelections.aspx" roles="members" />
        <siteMapNode title="Make Selections" description="Make Major Tournament Selections" url="~/members/makeSelections.aspx" roles="members" />
        <siteMapNode title="Entry Leader Boards" description="Tournament Entry Leaders" url="~/members/LeaderBoard.aspx" roles="members" />
        <siteMapNode title="Golfer Leader Boards" description="Tournament Golf Leaders" url="~/members/golfLeaders.aspx" roles="members" />
        <siteMapNode title="FAQ's" description="Frequently Asked Questions" url="~/faq.aspx" roles="*" />
     
    ............    
     
      </siteMapNode>
    </siteMap>

  4. #4
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Been lookinng around and a provider may be more than i want to tackle right now. I thought i could just create a function that would read the .sitemap file, but that doesn't seem the case.

    THis may have to be put on hold if there is not an easy way around it.

    Ras

  5. #5
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So i have been toolin around with it.

    If i put this in the Page_Load:

    Code Csharp:
    TreeView TreeView1 = (TreeView)LoginView2.FindControl("TreeView1");
    TreeNode tn = new TreeNode();
    tn.Text = "Test Node";
    TreeView1.Nodes.Add(tn);

    This does not add the node on the initial page load, but on postback, the node will apear. Then on every consecutive page loads, the node is added again.

    I tried putting it into a !IsPostBack but then the node never appeared.

    I tried putting it in the preinit segment and that didn't work either.

    Lastly i tried adding TreeView1.DataBind(); after the Nodes.Add and that did not work either.

    am i on the right track, or will doing it this way never work?

    Ras

  6. #6
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Csharp:
    TreeView TreeView1 = (TreeView)LoginView2.FindControl("TreeView1");
    TreeView1.DataSourceID = "SiteMapDataSource1";
    TreeView1.DataBind();
    TreeNode tn = new TreeNode();
    tn.Text = "Test Node";

    THis seems to do the trick, However, it is added as a root level node and that isn't quite where i want it. may just be a formating issue at this point.

  7. #7
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add this property to the .aspx page (on TreeView Control).....

    OnTreeNodePopulate="put method here"

    Then, you can wire it up to an event handler (function), and dynamically add nodes to the TreeView Control....hope it helps.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •