想做成那种显示层次结构的下拉菜单,结果输入的制表符变得不整齐,保存成html文档打开就正常,下面是代码和图:
Code
/// <summary>
/// 绑定父级部门下拉菜单
/// </summary>
private void bindFatherDepartment()
{
DataTable dt = creatDepartmentTable();
ddlFatherDepartment.DataSource = dt;
ddlFatherDepartment.DataTextField = "DepartmentName";
ddlFatherDepartment.DataValueField = "DepartmentID";
ddlFatherDepartment.DataBind();
ddlFatherDepartment.Items.Insert(0, "请选择……");
dt.Dispose();
}
/// <summary>
/// 构造作为数据源的datatable
/// </summary>
/// <returns></returns>
private DataTable creatDepartmentTable()
{
DepartmentsDS.DepartmentsDataTable result = new DepartmentsDS.DepartmentsDataTable();
DataSet DS = DBiz.getOrganizationList();
DataTable DT = DS.Tables[0];
for (int i = 0; i < DT.Rows.Count; i++)
{
result.Rows.Add(DT.Rows[i].ItemArray);
creatFatherDepartmentTable1(result, DT.Rows[i]["DepartmentID"].ToString());
}
DT.Dispose();
DS.Dispose();
return result;
}
/// <summary>
/// 添加带线的数据行
/// </summary>
/// <param name="target"></param>
/// <param name="ID"></param>
private void creatFatherDepartmentTable1(DataTable target,string ID)
{
DataSet DStemp = DBiz.getChildListByID(ID);
DataTable DTtemp = DStemp.Tables[0];
for (int i = 0; i < DTtemp.Rows.Count; i++)
{
DataRow DR = DTtemp.Rows[i];
//构造DR内容
DataRow tempDR = DR;
DataRow fatherDR = DBiz.getByID(tempDR["DepartmentSuperiorID"].ToString()).Tables[0].Rows[0];
for (int j = 0; j < (DR["DepartmentGUID"].ToString().Length/5 -1); j++)
{
if (j == 0)
{
if (Convert.ToInt32(tempDR["DepartmentOrder"]) < Convert.ToInt32(fatherDR["DepartmentCount"]))
{
DR["DepartmentName"] = "├ " +DR["DepartmentName"];
}
else
{
DR["DepartmentName"] = "└ " + DR["DepartmentName"];
}
}
else
{
if (Convert.ToInt32(tempDR["DepartmentOrder"]) < Convert.ToInt32(fatherDR["DepartmentCount"]))
{
DR["DepartmentName"] = "│ " + DR["DepartmentName"];
}
else
{
DR["DepartmentName"] = " " + DR["DepartmentName"];
}
}
if (fatherDR["DepartmentSuperiorID"]== DBNull.Value)
{
break;
}
tempDR = fatherDR;
fatherDR = DBiz.getByID(fatherDR["DepartmentSuperiorID"].ToString()).Tables[0].Rows[0];
}
target.Rows.Add(DR.ItemArray);
creatFatherDepartmentTable1(target, DR["DepartmentID"].ToString());
}
DStemp.Dispose();
}
问题补充:
/// <summary>
/// 绑定父级部门下拉菜单
/// </summary>
private void bindFatherDepartment()
{
DataTable dt = creatDepartmentTable();
ddlFatherDepartment.DataSource = dt;
ddlFatherDepartment.DataTextField = "DepartmentName";
ddlFatherDepartment.DataValueField = "DepartmentID";
ddlFatherDepartment.DataBind();
ddlFatherDepartment.Items.Insert(0, "请选择……");
dt.Dispose();
}
/// <summary>
/// 构造作为数据源的datatable
/// </summary>
/// <returns></returns>
private DataTable creatDepartmentTable()
{
DepartmentsDS.DepartmentsDataTable result = new DepartmentsDS.DepartmentsDataTable();
DataSet DS = DBiz.getOrganizationList();
DataTable DT = DS.Tables[0];
for (int i = 0; i < DT.Rows.Count; i++)
{
result.Rows.Add(DT.Rows[i].ItemArray);
creatFatherDepartmentTable1(result, DT.Rows[i]["DepartmentID"].ToString());
}
DT.Dispose();
DS.Dispose();
return result;
}
/// <summary>
/// 添加带线的数据行
/// </summary>
/// <param name="target"></param>
/// <param name="ID"></param>
private void creatFatherDepartmentTable1(DataTable target,string ID)
{
DataSet DStemp = DBiz.getChildListByID(ID);
DataTable DTtemp = DStemp.Tables[0];
for (int i = 0; i < DTtemp.Rows.Count; i++)
{
DataRow DR = DTtemp.Rows[i];
//构造DR内容
DataRow tempDR = DR;
DataRow fatherDR = DBiz.getByID(tempDR["DepartmentSuperiorID"].ToString()).Tables[0].Rows[0];
for (int j = 0; j < (DR["DepartmentGUID"].ToString().Length/5 -1); j++)
{
if (j == 0)
{
if (Convert.ToI
于为源
|
小虾三级
|
园豆:
956
提问于:2009-09-23 15:43