在做项目的时候,有时会遇到多级别分类(从数据库里取出)的问题,比如
一级分类:(必须存在)
二级分类:(可有可无)
条目:(如果二级分类别存在,在显示在二级分类下,不存在二级分类,显示在所属大类下)
需要实现的页面样式:
Math(大类)
Math1(小类)
M条目一 DropDownList(有A,B两项)
M条目二 DropDownList(有A,B两项)
English(大类,不存在二级类)
E条目一 DropDownList(有A,B两项)
E条目二 DropDownList(有A,B两项)
提交按钮
目的:从库中取出对应的大类,小类,和条目,用户选择下拉框,完成业绩考核(A优秀,B一般)
实现代码:
private void BindData()
{
// string grade_id = ViewState["grade_id"].ToString();
string grade_id = Request.QueryString["gradeid"];
DataTable dt_kindone=ScoreSystem.AllKindOne();
//数据库内有大类
if(dt_kindone.Rows.Count>0)
{
for (int i=0;i<dt_kindone.Rows.Count;i++)
{
string kindone_id=dt_kindone.Rows[i][SKindOneEntity.KINDONE_ID].ToString();
string kindone_name=dt_kindone.Rows[i][SKindOneEntity.KINDONE_NAME].ToString();
HtmlTableRow r_one=new HtmlTableRow();
HtmlTableCell c_one=new HtmlTableCell();
c_one.InnerHtml="<font size=3 style='TEXT-DECORATION:underline'><B>"+kindone_name+"</B></font>";
c_one.ColSpan=3;
r_one.Cells.Add(c_one);
this.tabledetail.Rows.Add(r_one);
//根据大类id查找相关小类
DataTable dt_kindtwo=ScoreSystem.KindTwoById(kindone_id);
if(dt_kindtwo.Rows.Count>0)
{
for(int j=0;j<dt_kindtwo.Rows.Count;j++)
{
string kindtwo_id=dt_kindtwo.Rows[j][SKindTwoEntity.KINDTWO_ID].ToString();
string kindtwo_name=dt_kindtwo.Rows[j][SKindTwoEntity.KINDTWO_NAME].ToString();
HtmlTableRow r_two=new HtmlTableRow();
HtmlTableCell c_two=new HtmlTableCell();
c_two.InnerHtml="<B> "+kindtwo_name+"</B>";
c_two.ColSpan=3;
r_two.Cells.Add(c_two);
this.tabledetail.Rows.Add(r_two);
//根据大类和小类id以及年级查找条目
DataTable Item_1=ScoreSystem.GetItemByAllId(kindone_id,kindtwo_id,grade_id);
if(Item_1.Rows.Count>0)
{
for(int m=0;m<Item_1.Rows.Count;m++)
{
string itemid_1=Item_1.Rows[m][SItemEntity.KINDITEM_ID].ToString();
string itemcontent_1=Item_1.Rows[m][SItemEntity.KINDITEM_NAME].ToString();
HtmlTableRow r_Item1=new HtmlTableRow();
//动态添加TextBox控件
HtmlTableCell c_txt1=new HtmlTableCell();
TextBox tb_1=new TextBox();
tb_1.ID="ItemId_m"+i.ToString()+j.ToString()+m.ToString();
tb_1.Text=itemid_1;
tb_1.Visible=false;
c_txt1.Controls.Add(tb_1);
r_Item1.Cells.Add(c_txt1);
HtmlTableCell c_Item1=new HtmlTableCell();
c_Item1.InnerHtml=itemcontent_1;
c_Item1.Width="280";
r_Item1.Cells.Add(c_Item1);
//动态添加DropDownList控件
HtmlTableCell c_Item1_drop=new HtmlTableCell();
DropDownList ddl_0=new DropDownList();
ddl_0.ID="ddl_m"+i.ToString()+j.ToString()+m.ToString();
ddl_0.Items.Add(new ListItem("I","I"));
ddl_0.Items.Add(new ListItem("P","P"));
ddl_0.Items.Add(new ListItem("E","E"));
c_Item1_drop.Controls.Add(ddl_0);
r_Item1.Cells.Add(c_Item1_drop);
this.tabledetail.Rows.Add(r_Item1);
}
}
}
}
else
{
//根据大类id查找Item
DataTable Item_2=ScoreSystem.GetItemByOneId(kindone_id,grade_id);
if(Item_2.Rows.Count>0)
{
for(int n=0;n<Item_2.Rows.Count;n++)
{
string itemid_2=Item_2.Rows[n][SItemEntity.KINDITEM_ID].ToString();
string itemcontent_2=Item_2.Rows[n][SItemEntity.KINDITEM_NAME].ToString();
HtmlTableRow r_Item2=new HtmlTableRow();
//动态添加TextBox控件
HtmlTableCell c_txt2=new HtmlTableCell();
TextBox tb_2=new TextBox();
tb_2.ID="ItemId_n"+i.ToString()+n.ToString();
tb_2.Text=itemid_2;
tb_2.Visible=false;
c_txt2.Controls.Add(tb_2);
r_Item2.Cells.Add(c_txt2);
HtmlTableCell c_Item2=new HtmlTableCell();
c_Item2.InnerHtml=itemcontent_2;
c_Item2.Width="280";
r_Item2.Cells.Add(c_Item2);
//动态添加DropDownList控件
HtmlTableCell c_Item2_drop=new HtmlTableCell();
DropDownList ddl_1=new DropDownList();
ddl_1.ID="ddl_n"+i.ToString()+n.ToString();
ddl_1.Items.Add(new ListItem("I","I"));
ddl_1.Items.Add(new ListItem("P","P"));
ddl_1.Items.Add(new ListItem("E","E"));
c_Item2_drop.Controls.Add(ddl_1);
r_Item2.Cells.Add(c_Item2_drop);
this.tabledetail.Rows.Add(r_Item2);
}
}
}
}
}
方法,利用动态生成控件的方法来完成,我觉得这种方法比较容易实现,如果用DataGrid,DataList等控件不容易控制,欢迎大家一起讨论。