很久以前写过一段代码:编码的时候生成规格,规格是根据中的ID 数据库对应规格,需要显示动态的文本框。
protected void dynamic_create_control()
{
string connectionString = "Data Source=SyteLine;Initial Catalog=SHATDB;Persist Security Info=True;User ID=SA;Password=SVTEP";
string SQLsettxt = "select Description,ID,Need,length from ItemCodeTypeDesc where TypeID=" + DropDownList2.Text + "Order by Priority ASC";
string SQLnum = "select count(ID) from ItemCodeTypeDesc where TypeID=" + DropDownList2.Text;
SqlConnection myConn = new SqlConnection(connectionString);
myConn.Open();
SqlCommand myCommnum = new SqlCommand(SQLnum, myConn);
imun = Convert.ToInt32(myCommnum.ExecuteScalar());
SqlCommand myComm = new SqlCommand(SQLsettxt, myConn);
SqlDataReader myDR = myComm.ExecuteReader();
int i = 0;
if (i < imun)
{
while (myDR.Read())
{
Label mylabel = new Label();
mylabel.ID = "lab" + (i + 1).ToString();
mylabel.Text = myDR[0].ToString() + ":";
mylabel.Width = 100;
mylabel.Height = 24;
Panel1.Controls.AddAt(i, mylabel);
TextBox tb = new TextBox();
tb.ID = "txt" + (i + 1).ToString();
if (myDR[0].ToString() == "外径")
{
tb.Text = "@";
}
else
{
tb.Text = "";
}
if (myDR["length"].ToString() == "")
{
tb.MaxLength = 50;
}
tb.MaxLength = Convert.ToInt32(myDR["length"].ToString());
string Need;
Need = myDR["Need"].ToString();
if (Need == "True")
{
tb.BackColor = System.Drawing.Color.Yellow;
}
else
{
tb.BackColor = System.Drawing.Color.White;
}
tb.Height = 15;
tb.Width = 100;
Panel2.Controls.AddAt(i, tb);
i++;
}
}
myDR.Close();
myConn.Close();
}