dropdownlist递归 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;public partial class Default3 : System.Web.UI.Page {
public DataSet ds = null; public DataTable table = new DataTable(); public SqlConnection con = null; public SqlDataAdapter ada = null; public string Kong = ""; //public int i = 0; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { con = new SqlConnection("server=192.168.0.21;database=zgag_sysframe;uid=sa;pwd=sa;"); ada = new SqlDataAdapter("select resid,resname,parentid from res", con); ds = new DataSet(); ada.Fill(ds); table = ds.Tables[0]; DataView view = new DataView(); view.Table = table; foreach (DataRow row in view.Table.Select("parentid='root'")) { ListItem lt = new ListItem(row["resname"].ToString(), row["resid"].ToString()); DropDownList1.Items.Add(lt); Method_In(DropDownList1.Items[DropDownList1.Items.Count-1].Value); } } } public void Method_In(string resid) { DataView view = new DataView(); view.Table = table; DataRow[] rows = view.Table.Select("parentid='"+resid+"'"); if (rows.Length > 0) { Kong = Kong + "-"; foreach (DataRow row in rows) { ListItem lt = new ListItem(Kong + row["resname"].ToString(), row["resid"].ToString()); DropDownList1.Items.Add(lt); Method_In(DropDownList1.Items[DropDownList1.Items.Count - 1].Value); } Kong = Kong.Remove(0, 1); } } }
{
this.test.InnerHtml = load().ToString();
} private StringBuilder load()
{
Category c1 = new Category(1, "一汽大众", 0);
Category c2 = new Category(2, "上海大众", 0);
Category c3 = new Category(3, "进口大众", 0);
Category c11 = new Category(4, "cc", 1);
Category c12 = new Category(5, "迈腾", 1);
Category c13 = new Category(6, "速腾", 1); Category c21 = new Category(7, "朗逸", 2);
Category c22 = new Category(8, "POLO", 2); Category c31 = new Category(9, "高尔夫", 3);
Category c32 = new Category(9, "尚酷", 3); List<Category> categories = new List<Category>();
categories.Add(c1);
categories.Add(c2);
categories.Add(c3);
categories.Add(c11);
categories.Add(c12);
categories.Add(c13);
categories.Add(c21);
categories.Add(c22);
categories.Add(c31);
categories.Add(c32);
StringBuilder sbHtml = new StringBuilder();
sbHtml.Append(@"<select name=""category"">");
foreach (Category parent in GetParent(categories, 0))
{
sbHtml.AppendFormat(@"<option id=""{0}"">{1}</option>", parent.Id, parent.CategoryName);
foreach (Category child in GetParent(categories, parent.Id))
{
sbHtml.AppendFormat(@"<option id=""{0}""> {1}</option>", child.Id, child.CategoryName);
}
}
sbHtml.Append("</select>");
return sbHtml;
} private List<Category> GetParent(List<Category> categories, int flag)
{
List<Category> getList = new List<Category>();
foreach (Category category in categories)
{
if (category.FlagId == flag) getList.Add(category);
}
return getList;
}
Category类 public class Category
{
public Category() { }
public Category(int id, string category, int flagId)
{
this.Id = id;
this.CategoryName = category;
this.FlagId = flagId;
} private int _id;
private string _categoryName;
private int _flagId;
//没写属性
}界面<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<div id="test" runat="server">
</div>
</body>
</html>
效果就是你上面的图片
sbHtml.Append(@"<option>请选择系列</option>");
比如我把这个分类写入数据库了!!把这个分类在大众CC,我进去修改就要默认是大众CC!!
跟那SelectedValue一样!!
给手动的 家空格转义然后给dropdownlist数据源
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
optgroup{
font-size:larger;
font-weight:bold;
}
</style>
</head><body>
<select>
<optgroup label="Swedish Cars">
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value ="mercedes">Mercedes</option>
<option value ="audi">Audi</option>
</optgroup>
</select>
</body>
</html>
select被封装成dropdownlist,楼主可以找找对应的方法。。
当然,你不用dropdownlist也可以,直接拼接html就好了,主要看楼主习惯不!
optgroup{
font-size:larger;
font-weight:bold;
font-style:italic;
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class Default3 : System.Web.UI.Page
{
public DataSet ds = null;
public DataTable table = new DataTable();
public SqlConnection con = null;
public SqlDataAdapter ada = null;
public string Kong = "";
//public int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con = new SqlConnection("server=192.168.0.21;database=zgag_sysframe;uid=sa;pwd=sa;");
ada = new SqlDataAdapter("select resid,resname,parentid from res", con);
ds = new DataSet();
ada.Fill(ds);
table = ds.Tables[0];
DataView view = new DataView();
view.Table = table;
foreach (DataRow row in view.Table.Select("parentid='root'"))
{
ListItem lt = new ListItem(row["resname"].ToString(), row["resid"].ToString());
DropDownList1.Items.Add(lt); Method_In(DropDownList1.Items[DropDownList1.Items.Count-1].Value); }
}
}
public void Method_In(string resid)
{
DataView view = new DataView();
view.Table = table;
DataRow[] rows = view.Table.Select("parentid='"+resid+"'");
if (rows.Length > 0)
{
Kong = Kong + "-";
foreach (DataRow row in rows)
{
ListItem lt = new ListItem(Kong + row["resname"].ToString(), row["resid"].ToString());
DropDownList1.Items.Add(lt);
Method_In(DropDownList1.Items[DropDownList1.Items.Count - 1].Value);
}
Kong = Kong.Remove(0, 1);
}
}
}
</cc1:SmartDropDownList>
pulic void ss(string car_Brand,string SmartDropDownList1 )
{SqlDataReader rd = DC.read("select Series_id,Series_name from Car_Series where series_Brand=" + car_Brand+ " ");
while (rd.Read())
{ ListItem li = new ListItem(rd[1].ToString(), "optgroup");
SmartDropDownList1.Items.Add(li); SqlDataReader rs = DC.read("select cars_id,cars_name from Car_cars where cars_series=" + rd[0] + "");
while (rs.Read())
{
li = new ListItem(rs[1].ToString(), rs[0].ToString());
SmartDropDownList1.Items.Add(li);
}
rs.Close();
}
rd.Close();
}
红色那个控件要怎么定义呢?