Setting The SelectNode.Value From Code-Behind File

3 posts, 1 answers
  1. Ahmad Alkhiary
    Ahmad Alkhiary avatar
    26 posts
    Member since:
    Jan 2004

    Posted 29 Nov 2008 Link to this post

    Dera Telerik Support,

    How can I set the SelectedNode.Value in the TreeView control which is inside the ComboBox Item Template from code behind file so it will show the node text in the ComboBox.

    In code behind I'm retrieving the data from the database and move it to the corresponding controls on the page and everything fine except the ComboBox. It may need passing a value to JavaScript function from code behind which will do the rest of the job.

    I added the following JavaScript function trying to accomplish the behavior


    function selectNode(SelectedValue)
    {
        if (SelectedValue != null)
        {
           var treevw = $find("<%= RadTreeView1.ClientID %>");
           var node = treevw.findNodeByValue(SelectedValue);
           node.select();

           var comboBox = $find("<%= RadComboBox1.ClientID %>");
           comboBox.set_text(node.get_text());
       }
    }


    But it produce the following error

     

    Compiler Error Message: BC30451: Name 'RadTreeView1' is not declared.

    Source Error:

    Line 31:        if (SelectedValue != null)
    Line 32:        {
    Line 33: var treevw = $find("<%= RadTreeView1.ClientID %>");Line 34:         var node = tree.findNodeByValue(SelectedValue);
    Line 35:         node.select();

    The following the ASPX page Code:

    <body bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0" dir=rtl>
        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
        </asp:ScriptManagerProxy>
     <script type="text/javascript">         
        function comboLoad(sender, eventArgs)
        {
            sender.set_text(sender.get_items().getItem(0).get_value());
        }

        function nodeClicking(sender, args)
        {
            var comboBox = $find("<%= RadComboBox1.ClientID %>");
            var node = args.get_node()
          
            document.getElementById('<%=NodeValue.ClientID%>').value = node.get_value();

            comboBox.set_text(node.get_text());
          

            comboBox.trackChanges();
            comboBox.get_items().getItem(0).set_value(node.get_text());
            comboBox.commitChanges();

            comboBox.hideDropDown();
        }

        function selectNode(SelectedValue)
        {
           if (SelectedValue != null)
           {
            var treevw = $find("<%= RadTreeView1.ClientID %>");
            var node = treevw.findNodeByValue(SelectedValue);
            node.select();
           
            var comboBox = $find("<%= RadComboBox1.ClientID %>");       
            comboBox.set_text(node.get_text());
           }       
        }

        function StopPropagation(e)
        {
            if(!e)
            {
                e = window.event;
            }

            e.cancelBubble = true;
        }
       
        function OnClientDropDownOpenedHandler(sender, eventArgs)
        {
            var tree = sender.get_items().getItem(0).findControl("RadTreeView1");
            var selectedNode = tree.get_selectedNode();
            if (selectedNode)
            {
                selectedNode.scrollIntoView();
            }
        }
      
    </script>
    <telerik:RadComboBox ID="RadComboBox1" Runat="server" Height="200px"
        Skin="Sunset" Width="348px">
        <ItemTemplate>
          <div ID="TreeDiv" onclick="StopPropagation(event)" dir=rtl>
            <telerik:RadTreeView
             ID="RadTreeView1"
             Runat="server"
             Height="350px"
             Width="100%"
             OnClientNodeClicking="nodeClicking" Skin="Sunset">
                <CollapseAnimation Duration="100" Type="OutQuint" />
                <ExpandAnimation Duration="100" />
            </telerik:RadTreeView>
            </div>
        </ItemTemplate>
        <Items>
          <telerik:RadComboBoxItem Text="" />
        </Items>
    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
    </telerik:RadComboBox>
    <input id="NodeValue" value="0" runat="server" enableviewstate="true" type="hidden" />
    <input id="SelectedValue" value="0" runat="server" enableviewstate="true" type="hidden" /> 
    </body>

    Thank you

  2. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 02 Dec 2008 Link to this post

    Hello Ahmad Alkhiary,

    You are not getting the proper reference to the treeview object.

    Please check our online example and help article on how to do this.

    I hope this helps.

    Kind regards,
    Veselin Vasilev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 31 Jul 2009 Link to this post

    Thanks, the second link helped me with a similar problem.
Back to Top