1、动态生成控件
//调用webservice方法
Service1 sc = new Service1();
DataTable dt = sc.QueryTable("student");
DataTable dtDes = sc.CloumnDescription(dt.TableName);//该方法为获取表中每个字段的说明
for (int i = 0; i < dtDes.Rows.Count; i++)
{
string[] str= new string[2];
for (int j = 0; j < dtDes.Columns.Count; j++)
{
str[j] = dtDes.Rows[i][j].ToString();
}
//生成label
Label lb = new Label();
lb.Name = "lb" + i.ToString();//区分各个label
lb.Text = str[0];
lb.Width = 80;
System.Drawing.Point p = new Point(10, 10 + i * 30);//label的放置坐标
lb.Location = p;
panel1.Controls.Add(lb);//把lable放到panel里面
//生成textbox
TextBox tx = new TextBox();
tx.Name = "tx" + i.ToString();//区分各个textbox
tx.Text = str[1];
tx.ReadOnly = true;
System.Drawing.Point p1 = new Point(90, 10 + i * 30);//textbox的放置坐标
tx.Location = p1;
panel1.Controls.Add(tx);//把textbox放到panel里面
}
2、批量修改控件属性
//把panel1里面的所有textbox设置为只读状态
foreach (Control control in panel1.Controls)
{
((TextBox)control).ReadOnly = true;
}