C#动态添加控件

能动态添加就是不怎会定位....

 
 for (j = 0; j < ds.Tables[0].Rows.Count; j++)//ds为Dataset的对象,
      {
            
string strname1 = ds.Tables[0].Rows[j][0].ToString();

            HtmlGenericControl div 
= new HtmlGenericControl("div");
            div.Style.Add(
"width""100px");
            div.Style.Add(
"height""40px");

            Label lab 
= new Label();
            TextBox txt 
= new TextBox();
            txt.ID 
= "txt_object" + j.ToString();
            lab.Text 
= strname1 + "";
            txt.Width 
= Unit.Pixel(50);
            div.Controls.Add(lab);
            div.Controls.Add(txt);

            
this.Page.Form.Controls.Add(div);
        }

得到文本框里面得值

 protected void Button1_Click(object sender, EventArgs e)
{
           
int i = 1;//假定i为1

           objText 
= ((TextBox)form1.FindControl("txt_object" + i.ToString())).Text.ToString();
           Response.Write(objText);
    }

 

posted @ 2014-05-24 12:17  zzg168  阅读(287)  评论(0)    收藏  举报