您当前的位置: 首页 >  .net

[Asp.Net]动态生成控件

发布时间:2008-10-08 14:43:00 ,浏览量:0

 很久以前写过一段代码:编码的时候生成规格,规格是根据中的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();
    }
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    109966博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.3293s