creating a hierarchical dropdownlist in asp.net
i am working on eCommerce site in asp.net 3.5.in that i have to create categories.The categories is Stored as hierarchical order based on parent Id.Because the level can increase.
my problem is in add/Edit Categories page.in that categories has to display in hierarchy.
i was searching in Google for any help to create a hierarchical drop down list in asp.net.but i didn't get it.instead i found http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm.
i am confused if i used the li开发者_高级运维st instead of dropdownlist. how can i get the value of selected item in code behind while saving.and how to select the item on category edit.
please help to sort out this.
for this you have to use recursion once I created it you can check my code. You can modify according to your requirement.
public void FillDDLOfParentCat()
{
DataTable dt = new DataTable();
dt = dal.GetDataTable("SELECT * FROM Category_Master WHERE Parent='0' ORDER BY Name");
ddl_category.Items.Add("--Select category--");
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["PK_ID"].ToString());
ddl_category.Items.Add(li);
PrintChild(Convert.ToInt32(dt.Rows[i]["PK_ID"].ToString()), "----");
}
}
}
public void PrintChild(int pk_id, string Space)
{
DataTable dt = new DataTable();
dt = dal.GetDataTable("SELECT * FROM Category_Master WHERE Parent='" + pk_id + "' ORDER BY Name");
string sp = new string(Space.ToCharArray());
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = new ListItem(sp + dt.Rows[i]["Name"].ToString(), dt.Rows[i]["PK_ID"].ToString());
ddl_category.Items.Add(li);
PrintChild(Convert.ToInt32(dt.Rows[i]["PK_ID"].ToString()), "----" + sp);
}
}
}
I'm not sure whether this is an option for you, but the RadComboBox from Telerik is extremely flexible. Here's an example where I embedded a tree view in a RadComboBox:
<telerik:RadComboBox ID="DropDownList1" runat="server" CausesValidation="false" Skin="Sunset" AutoPostBack="true" Width="500" Font-Size="12px" Font-Names="Verdana" OnClientDropDownOpened="onDropDownOpened">
<ItemTemplate>
<div style="padding:0px 0px 5px 5px;">
<telerik:RadTreeView ID="TreeView1" runat="server" CausesValidation="false" Skin="Sunset" Font-Size="12px" Font-Names="Verdana" OnNodeClick="TreeView1_NodeClick" PersistLoadOnDemandNodes="true" ShowLineImages="true" OnClientNodeClicking="onTreeNodeClicking" />
</div>
</ItemTemplate>
<Items>
<telerik:RadComboBoxItem Text="" />
</Items>
</telerik:RadComboBox>
精彩评论