下拉框DropDownList绑定数据,树状列表显示
在下拉列表框里显示的结果如下
/// <summary>
/// 绑定生成一个有树结构的下拉菜单
/// </summary>
/// <param name="dtNodeSets">菜单记录数据所在的表</param>
/// <param name="strParentColumn">表中用于标记父记录的字段</param>
/// <param name="strRootValue">第一层记录的父记录值(通常设计为0或者-1或者Null)用来表示没有父记录</param>
/// <param name="strIndexColumn">索引字段,也就是放在DropDownList的Value里面的字段</param>
/// <param name="strTextColumn">显示文本字段,也就是放在DropDownList的Text里面的字段</param>
/// <param name="drpBind">需要绑定的DropDownList</param>
/// <param name="i">用来控制缩入量的值,请输入-1</param>
private void MakeTree(DataTable dtNodeSets,string strParentColumn,string strRootValue,string strIndexColumn,string strTextColumn,DropDownList drpBind,int i)
{
}
调用时,MakeTree(dt, "parentid", "", "sid", "servname", ddlCategory, -1);
第二种:
{
if (!Page.IsPostBack)
{
//绑定一级分类
DataSet ds=resource.GetFirstCategoryByUserDs();
foreach(DataRow dr in ds.Tables[0].Rows)
{
ListItem li =new ListItem("├"+ dr["TypeName"].ToString(),dr["TypeId"].ToString());
ddlCategory.Items.Add(li);
CategoryBind(resource.GetCategoryByUserTypeDs(dr["TypeId"].ToString()), 0);
}
}
}
public void CategoryBind(DataSet ds,int i)
{
i++;
string strPading = "";
for (int j = 0; j < i; j++)
strPading += " ";
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
foreach(DataRow dr in ds.Tables[0].Rows)
{
ListItem li =new ListItem(strPading + "├" + dr["TypeName"].ToString(),dr["TypeId"].ToString());
ddlCategory.Items.Add(li);
CategoryBind(resource.GetCategoryByUserTypeDs(dr["TypeId"].ToString()), i);
}
}
i--;
}