Solved

Tree-like Dropdownlist

Posted on 2010-08-17
8
592 Views
Last Modified: 2013-11-08
Is there a way in ASP.NET to have a tree like dropdownlist. See example below. The dropdown list control could have data like -

All Products
Drinks
Snacks

But then when the user clicks on 'Drinks', it should then show -

All Products
Drinks
       Coke
       Diet Coke
       .....
Snacks

The above data lives in the database, so also looking for suggestions for data retrieval - do we call a stored proc every time (like when you click 'Drinks' or 'Snacks') . Am using AJAX,/VS2008 so preferably any solution with code would help.  Thanks.
0
Comment
Question by:tapdev72
8 Comments
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33458565
can u please tell what you have to do a treelike drop down list ?
0
 
LVL 4

Expert Comment

by:avarmaavarma
ID: 33458680
There are several 3rd party controls (e.g. Telerik included below) that will let you do this.

http://www.telerik.com/community/forums/aspnet-ajax/treeview/multiselect-treeview-inside-dropdown.aspx

In order to code it yourself inside a WinForms dropdownlist, you can either use a hack - which is to add a succession of spaces to make the string look like it is 'nested'
(http://forums.asp.net/p/1003245/1325999.aspx#1325999)

OR you can combine a textbox with a downarrow (to make it look like a dropdown list) and then use the selection of the textbox to populate an actual treeview control. See this example for how this might be done:

http://technoblizzard.blogspot.com/2007/09/tree-view-in-dropdownlist.html
0
 
LVL 2

Expert Comment

by:larkvale
ID: 33461927

Check out this control from DevComponents

http://devcomponents.com/dotnetbar/ComboBoxTreeControl.aspx


0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:tapdev72
ID: 33462696
Not looking for 3rd party components.
0
 
LVL 14

Accepted Solution

by:
Dhanasekaran Sengodan earned 500 total points
ID: 33464133
Create a Dropdownlist
===================
<asp:DropDownList ID="ddlCategories" runat="server" CssClass="nav_Dashboard" AppendDataBoundItems="true">
<asp:ListItem Text="Select Category" Value="0"></asp:ListItem>
</asp:DropDownList>

the string "strcolor " shows the different root level

The DB design

CategoryID    Category     ParentCategoryID
=================================
1                     Root                            0
2                     SubCat                         1
3                     SubCat2                        1
4                     SubCat11                      2
5                      SubCat22                     2
6                     Root2                           0
7                       SubCat 21                  6
----------------------------------------------------------


public string text = "";
	string[] strcolor = { "color:#006400;", "color:#0000FF;", "color:#8B0000;", "color:#800080;", "color:#D2691E;", "color:#4B0082;", "color:#9932CC;" };
public DataTable dt = null;
 protected void Page_Load(object sender, EventArgs e)
	{   
 if(!IsPostBack)
    {
		dt = // Get the value from DB
		text = "";
		CreateList("0");
    }
}
	private void CreateList(string parentID)
	{
		DataRow[] drCollection = dt.Select("ParentCategoryID='" + parentID + "'");
		foreach (DataRow drRow in drCollection)
		{
			text = "";
			int nodelevel = Convert.ToInt32(drRow["level"].ToString());
			for (int i = 0; i < nodelevel; i++)
			{
				if (parentID == "0")
				{
					text = "";
				}
				else
				{
					text += "--";
				}
			}
			if (text == "")
			{
				ListItem objLI = new ListItem();
				objLI.Text = text + drRow["Category"].ToString();
				objLI.Value = drRow["CategoryID"].ToString();
				objLI.Attributes.Add("Style", strcolor[nodelevel - 1]);
				ddlCategories.Items.Add(objLI);
			}
			else
			{
				ListItem objsubLI = new ListItem();
				objsubLI.Text = "|" + text + drRow["Category"].ToString();
				objsubLI.Value = drRow["CategoryID"].ToString();
				objsubLI.Attributes.Add("Style", strcolor[nodelevel - 1]);
				ddlCategories.Items.Add(objsubLI);
			}
			DataRow[] drChilds = dt.Select("ParentCategoryID='" + drRow["CategoryID"].ToString() + "'");
			if (drChilds.Length > 0)
			{
				CreateList(drRow["CategoryID"].ToString());
			}
		}
	}

Open in new window

0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33464182
i forgot to add stored procedure. I added below
Create Procedure [dbo].[GetallCategories]
as

WITH q (categoryid, parentcategoryid, category, level, bc) AS
        (
        SELECT CategoryID, ParentCategoryID, Category, 1, CAST(ROW_NUMBER() OVER (ORDER BY category) AS VARCHAR(MAX))
        FROM SA_FAQCategory WHERE parentcategoryid=0 
        UNION ALL
        SELECT c.CategoryID, c.ParentCategoryID, c.Category, q.level + 1, q.bc + '.' + CAST(ROW_NUMBER() OVER (ORDER BY q.level) AS VARCHAR(MAX))
        FROM q JOIN SA_FAQCategory c ON c.parentcategoryid = q.categoryid
        )
SELECT * FROM q ORDER BY bc

Open in new window

0
 
LVL 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33464226
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now