下拉框层级绑定
今天用到一个下拉框绑定,要有层级关系显示的,也很简单,就当第一篇博客!
/// <summary>
/// 绑定下拉框
/// </summary>
/// <param name="dt">绑定的数据源</param>
/// <param name="parentId">上一级的id名称</param>
/// <param name="colId">值名称</param>
/// <param name="colName">文本名称</param>
/// <param name="drs">最上层的记录集合</param>
/// <param name="ddl">下拉框</param>
/// <param name="leveStr">层级的前缀</param>
/// <param name="nextStr">层级的前缀的前缀</param>
protected void GetChild(DataTable dt, string parentId, string colId, string colName, DataRow[] drs, DropDownList ddl, string leveStr, string nextStr)
{
foreach (DataRow dr in drs)
{
string txt = dr[colName].ToString();
if (leveStr.Length != 1)
{
txt = leveStr + dr[colName].ToString();
}
ddl.Items.Add(new ListItem(txt, dr[colId].ToString()));
DataRow[] cdrs = dt.Select(parentId + " ='" + dr[colId] + "'", colId + " asc");
if (cdrs.Length != 0)
{
string nextLevelStr = leveStr.Insert(0, nextStr);
GetChild(dt, parentId, colId, colName, cdrs, ddl, nextLevelStr, nextStr);
}
}
}
drs通过dt.select(“条件”)查询。
摘自 Ne
补充:Web开发 , ASP.Net ,