protected void Page_Load(object sender, EventArgs e)
{
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Width = Unit.Percentage(50);
TableRow trHead = new TableRow();
TableCell tcHeadFoodName = new TableCell();
TableCell tcHeadType = new TableCell();
tcHeadFoodName.Text ="类别";
tcHeadType.Text = "值";
trHead.Cells.Add(tcHeadFoodName);
trHead.Cells.Add(tcHeadType);
trHead.HorizontalAlign = HorizontalAlign.Center;
trHead.Height = 30;
TableRow tr2 = new TableRow();
TableCell tc21 = new TableCell();
TableCell tc22 = new TableCell();
tc21.Text = "产品名称:";
TextBox tb1 = new TextBox();
tb1.ID = "txtProdName";
tc22.Controls.Add(tb1);
tr2.Cells.Add(tc21);
tr2.Cells.Add(tc22);
Table1.Rows.Add(tr2);
SqlConnection conn = DB.CreateConn();
conn.Open();
int pid=Convert.ToInt32(Request.QueryString["classid"].ToString());
lblClassid.Text = Request.QueryString["classid"].ToString();
string sql = "select t.* from(select * from tb_class where parentid=" +pid+ " and isclass=0 union all select * from tb_class where parentid in(select classID from tb_class where parentid=3 and isclass=0)) t order by dbo.f_getClassmergid(t.classid)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{//开始动态创建控件
if (Convert.ToInt32(dr["parentid"].ToString()) == pid)
{//判断是类别,则不创建控件。
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
tc1.Text = dr["className"].ToString();
tr1.BackColor = Color.LightGray;
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
Table1.Rows.Add(tr1);
}
else
{
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
Label lbl = new Label();
lbl.ID = "lbl" + dr["classid"].ToString();
lbl.Text = dr["className"].ToString();
TextBox tb = new TextBox();
tb.ID = "txt" + dr["classid"].ToString();
tc1.Controls.Add(lbl);
tc2.Controls.Add(tb);
//验证控件
RequiredFieldValidator rf = new RequiredFieldValidator();
rf.ID = "rs" + dr["classid"].ToString();
rf.ControlToValidate = "txt" + dr["classid"].ToString();
rf.ErrorMessage = "请输入"+dr["className"].ToString();
tc2.Controls.Add(rf);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tr1.HorizontalAlign = HorizontalAlign.Left;
tr1.Height = 30;
tr1.VerticalAlign = VerticalAlign.Middle;
Table1.Rows.Add(tr1);
}
}
dr.Close();
conn.Close();
conn.Dispose();
}