Multiple select TreeView in ComboBox

Share

The Telerik ComboBox for ASP.NET allows you to define an ItemTemplate which gives you the possibility to customize the appearance and functionality of the ComboBox. This article provides an example on how to place a multi-select TreeView inside an ItemTemplate, handling the selection on client side and how to process the data on the server side.

Telerik provides an example of placing a TreeView in a ComboBox here . But what if you want a multiple selection TreeView (with tri-state check boxes) ?
The ComboBox with the multi-select TreeView described in this article looks like this .

Fig. 1 Multi-select TreeView in ComboBox

We are going to begin with the .aspx page and the Javascript that handles TreeView selection on the client side.

The code that needs to be placed inside the .aspx page :

As you can see from the code above, we’re not adding the TreeView directly to the ItemTemplate, instead we add a div with a click handler and we place the TreeView inside this div element. This is required to stop the drop down from closing when the user clicks on a node from the TreeView.
You can find an example and some explanations of the properties used in a TreeView with check boxes here.
We are going to focus on the nodeChecked and nodeCliked but before we look at those functions, note that we added a node to the tree to allow selecting/deselecting all the nodes at once, and also, in the Items collection of the ComboBox we have to add an item with no text (this is needed for the tree to be shown).

When a node is checked, the text displayed in that node will be added to the ComboBox Text property. This is achieved using the following Javascript code :

As you can see, when a node from the tree is clicked, we select/deselect the node and call the nodeChecked function. The ‘nodeChecked’ funtion checks if the node containing the “Select All” text has been clicked, if this is the case you can select or deselect all the nodes from the tree (this code has been omitted for simplicity).
If the user clicked another node we loop through the collection of checked nodes and store all the text displayed by this nodes in a local variable that will be used to set the ‘Text’ property of the ComboBox.
If you remember, when we declared the combo box , the TreeView was added inside a div element witch had a click handler associated. When the user performs a click on the div element (this means every time the user selects a node from the TreeView) the StopPropagation function that will cancel the event bubbling will be called.

Moving to the server side, you can bind the TreeView to a data source and you can also change the mode nodes are displayed. To bind a tree to a data source you need the following fields : DataFieldID , DataFieldParentID , DataTextField , DataValueField . The TreeView object can be accesed from the ComboBox by calling FindControl with the id of the tree on the first item of the combo box. Here’s an example of how you can do that :

If you want to add a node on the first level (with no parents) the DataFieldParentID field must be empty .
In the image from the beginning of the article you can see that all the nodes are checked and the nodes that have children are displayed with bold font. All that is done on the combo box Load event and here is the code :

To get the value of a checked node you can do the following :

The above method returns a string with the ids of the selected nodes (the value that is bound to the DataValueField property) separated by a comma. You could also get the text displayed in the node using :

That’s it, hope you find this useful.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

3 Comments
  • Sudarshan says:

    Can you please tell how to use it JSP page ?

    Thanks

    January 6, 2012 at 9:10 am
  • R.Ganesan says:

    OnClientDropDownClosed=”OnClientDropDownClosing” == event is creating problem

    January 6, 2012 at 3:02 pm
  • googlepo says:

    good content

    September 11, 2012 at 2:25 am

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close