This is a migrated thread and some comments may be shown as answers.

ComboBox with Treeview copy values from one combobox to the other

1 Answer 43 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Aleksander
Top achievements
Rank 1
Aleksander asked on 02 Feb 2012, 08:55 PM
I have 2 combo boxes with treeviews in them for a States drop down list and when I click a button, I would like the first combobox to copy the value and text to the second one as well as select the appropriate node and expand to it if needed. The combo box is a list of states and then the only parent is Territories which then shows the US territories. The code I have is as follows.

My markup is as follows...

function nodeClicking(sender, args) {
            var temp = sender._clientStateFieldID.split('_');
            if (temp.length >= 1) {
                var name = temp[0];
                var comboBox = $find(name);
 
                var node = args.get_node();
 
                comboBox.set_text(node.get_text());
 
                comboBox.trackChanges();
                comboBox.get_items().getItem(0).set_text(node.get_text());
                comboBox.get_items().getItem(0).set_value(node.get_value());
                comboBox.commitChanges();
 
                comboBox.hideDropDown();
            }
        }
 
<div style="width: 100%;">
            <tk:RadComboBox ID="rcbState" runat="server" Width="250px" ShowToggleImage="true" EmptyMessage="Choose a State"
                HighlightTemplatedItems="true" style="vertical-align: middle;" >
                <ItemTemplate>
                    <div id="div1">
                        <tk:RadTreeView ID="rtvState" runat="server" OnClientNodeClicking="nodeClicking">
                            <DataBindings>
                                <tk:RadTreeNodeBinding Expanded="false" />
                            </DataBindings>
                        </tk:RadTreeView>
                    </div>
                </ItemTemplate>
                <Items>
                    <telerik:RadComboBoxItem Text="" />
                </Items>
            </tk:RadComboBox>
            <asp:Label ID="lblTest" runat="server" />
        </div>
 
        <div>
            <tk:RadComboBox ID="rcbState2" runat="server" Width="250px" ShowToggleImage="true" EmptyMessage="Choose a State"
                HighlightTemplatedItems="true" style="vertical-align: middle;" >
                <ItemTemplate>
                    <div id="div1">
                        <tk:RadTreeView ID="rtvState2" runat="server" OnClientNodeClicking="nodeClicking">
                            <DataBindings>
                                <tk:RadTreeNodeBinding Expanded="false" />
                            </DataBindings>
                        </tk:RadTreeView>
                    </div>
                </ItemTemplate>
                <Items>
                    <telerik:RadComboBoxItem Text="" />
                </Items>
            </tk:RadComboBox>
            <asp:Label ID="lblTest2" runat="server" />
        </div>
 
        <div>
            <tk:RadButton ID="btnTest" runat="server" Text="Submit" OnClick="btnTest_Click" />
        </div>

Then code behind is...

protected void btnTest_Click(object sender, EventArgs e)
    {
        string text = rcbState.SelectedItem.Text;
        string value = rcbState.SelectedItem.Value;
 
        lblTest.Text = "Name = " + text + ", ID = " + value;
 
        rcbState2.Text = text;
 
        RadTreeView rtv2 = rcbState2.Items[0].FindControl("rtvState2") as RadTreeView;
 
        (rtv2.Nodes.FindNodeByValue(value)).Expanded = true;
 
        if (rcbState2.SelectedItem != null)
        {
            lblTest2.Text = rcbState2.SelectedItem.Text;
            lblTest2.Text = rcbState2.SelectedItem.Value;
        }
    }

I'm having 2 problems... 1 is when i click the button the first time it'll get the text and value from the combo box but on the second time around it's null. 2 is I can't get the second state combobox to set the text and value and the selected item value.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Feb 2012, 10:53 AM
Hello,

Try the following code snippet.
C#:
protected void Button1_Click(object sender, EventArgs e)
   {
      string value = RadComboBox1.SelectedItem.Value;
       string text = RadComboBox1.SelectedItem.Text;
       RadComboBox2.SelectedItem.Text = text;
       RadComboBox2.SelectedValue = value;
       RadTreeView RadTreeView2= RadComboBox2.Items[0].FindControl("RadTreeView2") as RadTreeView;
       if (RadTreeView2.FindNodeByText(text).Level>0) //to check whether it is a parent node or not
       {
           RadTreeView2.FindNodeByText(text).ParentNode.Expanded = true;
           RadTreeView2.FindNodeByText(text).Selected = true;
       }
       else
       {
           RadTreeView2.FindNodeByText(text).Selected = true;
       }
   }

Thanks,
Princy.
Tags
ComboBox
Asked by
Aleksander
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or