Assigning and retrieving id of nodes

7 posts, 0 answers
  1. Zeus_developer
    Zeus_developer avatar
    14 posts
    Member since:
    Jun 2006

    Posted 29 May 2012 Link to this post

    Hi,

    I'm trying to load a treeview from this xml string:

      <?xml version="1.0" encoding="utf-8" ?>
    - <Tree>
      <Node Text="TAREAS REPETITIVAS: botón reminder para quienes no entregan adjunto" ID="1311" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True" />
      <Node Text="TAREAS REPETITIVAS: Diferenciar archivos x usuario (en carpetas?)" ID="1312" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True" />
      <Node Text="MENSAJES PRIVADOS: convertirlo a cloud?" ID="1318" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True" />
      </Tree>

    as you can see, each node have Text and ID attributes
    sometimes, the nodes will have childs (only nodes two levels: parent - child)

    when a linkbutton be pressed, I want to iterate the nodes and retrieving the id of each node and the id of the node childs 

    I'm not sure if this is the best way to do this functionality... could you help me please?
  2. Zeus_developer
    Zeus_developer avatar
    14 posts
    Member since:
    Jun 2006

    Posted 31 May 2012 Link to this post

    anyone? please help
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 01 Jun 2012 Link to this post

    Hello Segundo,

    You can store the ID as Value using the ValueField property of RadTreeNodeBinding. Here is an example:

    aspx:
    <asp:button runat="server" text="Button" onclick="Button1_Click" />
       <telerik:RadTreeView ID="RadTreeView2" runat="server" DataSourceID="XmlDataSource1">
           <DataBindings>
               <telerik:RadTreeNodeBinding DataMember="Node" TextField="Text" ValueField="ID" ImageUrlField="ImageUrl"
                   ExpandedField="Expanded" />
           </DataBindings>
       </telerik:RadTreeView>
       <asp:xmldatasource runat="server" id="XmlDataSource1" datafile="TreeView.xml" xpath="/Tree/Node" />

    code-behind:
    protected void Button1_Click(object sender, EventArgs e)
       {
           foreach (RadTreeNode item in RadTreeView2.GetAllNodes())
           {
               Response.Write(item.Value + "-");
           }
       }

    treeview.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <Tree>
      <Node Text="Desktop" ID="1" Expanded="True" ToolTip="Desktop" ImageUrl="~/TreeView/Img/Vista/desktop.png">
        <Node Text="Administrator" ID="2" ImageUrl="~/TreeView/Img/Vista/folder-personal.png" Expanded="True">
          <Node Text="AppData" ID="3" ImageUrl="~/TreeView/Img/Vista/folder.png">
            <Node Text="Microsoft" ID="4" ImageUrl="~/TreeView/Img/Vista/folder.png"/>
          </Node>
          <Node Text="Contacts" ImageUrl="~/TreeView/Img/Vista/folder-contacts.png" ID="5" />
          <Node Text="Downloads" ImageUrl="~/TreeView/Img/Vista/folder-open.png" ID="6"/>
          <Node Text="Documents" ImageUrl="~/TreeView/Img/Vista/folder-documents.png" Selected="True" ID="7" />
          <Node Text="Favorites" ImageUrl="~/TreeView/Img/Vista/folder-favorites.png" ID="8">
            <Node Text="Links" ImageUrl="~/TreeView/Img/Vista/folder.png" ID="9"/>
          </Node>
          <Node Text="Music" ImageUrl="~/TreeView/Img/Vista/folder-music.png" ID="10" />
          <Node Text="Pictures" ImageUrl="~/TreeView/Img/Vista/folder-pictures.png" ID="11" />
          <Node Text="Saved Games" ImageUrl="~/TreeView/Img/Vista/folder-open.png"  ID="12"/>
          <Node Text="Searches" ImageUrl="~/TreeView/Img/Vista/searches.png" ID="13">
            <Node Text="History" ImageUrl="~/TreeView/Img/Vista/folder.png" ID="14"/>
          </Node>
          <Node Text="Videos" ImageUrl="~/TreeView/Img/Vista/folder-videos.png" ID="15"/>
        </Node>
        <Node Text="Computer" ToolTip="My Computer" ImageUrl="~/TreeView/Img/Vista/computer.png" Expanded="True" ID="16">
          <Node Text="WebServer (\\10.0.0.80) (W:)" ImageUrl="~/TreeView/Img/Vista/network-drive.png" ID="17"/>
          <Node Text="Local Disk (C:)" ImageUrl="~/TreeView/Img/Vista/system-drive.png" Expanded="True" ID="18">
            <Node Text="inetpub" ImageUrl="~/TreeView/Img/Vista/folder.png" Expanded="True" ID="16">
              <Node Text="AdminScripts" ImageUrl="~/TreeView/Img/Vista/folder.png" ID="19"></Node>
            </Node>
          </Node>
        </Node>
      </Node>
    </Tree>

    Attached is a sample for reference.


    Greetings,
    Peter
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  5. Zeus_developer
    Zeus_developer avatar
    14 posts
    Member since:
    Jun 2006

    Posted 01 Jun 2012 Link to this post

    Hi Peter,

    thanks for your response, it works fine... but it doesn't work when the xml is loaded from a string...I need to load the xml dinamically, then I can't use the xmlDatasource.

    This is my code:

    <telerik:RadTreeView ID="RadTreeView1" runat="server"  EnableDragAndDrop="True"
        MultipleSelect="true" EnableDragAndDropBetweenNodes="False">
       <DataBindings>
           <telerik:RadTreeNodeBinding DataMember="Node" TextField="Text" ValueField="ID" ImageUrlField="ImageUrl"
               ExpandedField="Expanded" />
       </DataBindings>
    </telerik:RadTreeView>
    RadTreeView1.LoadXml(xmlAsString)
    RadTreeView1.ExpandAllNodes()
    RadTreeView1.MultipleSelect = False
    <?xml version="1.0" encoding="utf-8" ?>
    <Tree>
    <Node Text="TAREAS REPETITIVAS: botón reminder para quienes no entregan adjunto" ID="1311" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True"></Node>
    <Node Text="TAREAS REPETITIVAS: Diferenciar archivos x usuario (en carpetas?)" ID="1312" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True"></Node>
    <Node Text="MENSAJES PRIVADOS: convertirlo a cloud?" ID="1318" Expanded="True" ImageUrl="~/Images/check.png" IsFather="True"></Node>
    </Tree>
    Protected Sub lnkSaveChanges_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkSaveChanges.Click
     
        For Each node As RadTreeNode In RadTreeView1.GetAllNodes()
            Dim ParentID As String = node.Value
     
            For Each nodeChild As RadTreeNode In node.GetAllNodes()
                Dim ChildID As String = nodeChild.Value
     
            Next
     
        Next
     
    End Sub

    please give it a try and tell me why the radtreeNode isn't getting the ID attribute.

    I'm attaching an image

    Regards
  6. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 06 Jun 2012 Link to this post

    Hello Segundo,

    I tried to workaround the problem but to no avail. DataBindings work only with declarative data sources. Unfortunately, your scenario is not supported.

    Kind regards,
    Peter
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  7. Marbry
    Marbry avatar
    26 posts
    Member since:
    Aug 2012

    Posted 06 Jun 2012 Link to this post

    Does it even return the ID in the node's attribute collection?  e.g. can you use getAttribute to retrieve it?
  8. Peter
    Admin
    Peter avatar
    6637 posts

    Posted 11 Jun 2012 Link to this post

    Hi Marby,

    The ID will not be stored in the attributes collection of the menu item in the scenario described here.

    All the best,
    Peter
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017