在cs 中动态生成页面的控件,并进行操作
1)页面添加一个按钮
2)在Page_Load中执行CreateTab,保证你在刷新叶面时,动态表格不会消失,如果在按钮单击事件中用CreateTab,就会发现每次按钮会重画表格
private void Page_Load(object sender, System.EventArgs e)
??{
???if(!Page.IsPostBack)
???{
??
???}
???else
???{
???????CreateTab(TextBox1.Text,TextBox2.Text);
???}
???
???// 在此处放置用户代码以初始化页面
??}
3)生成动态表格按钮单击事件不需要写任何代码
private void Button1_Click(object sender, System.EventArgs e)
??{
??}
4)生成动态表格
??///
??/// 生成动态表格
??///
??/// 行数
??/// 列数
??private void CreateTab(string rows,string cols)
??{
???if(rows!=""&&cols!="")??
???{
????Table table1=new Table();
????
????for (int i=0;i
????{
?????TableRow tr=new TableRow();
?????for (int j=0;j
?????{
??????TableCell td=new TableCell();
??????td.Width=100;
????
??????TextBox txt=new TextBox();
??????txt.ID="txt"+i.ToString()+j.ToString();
??????txt.Width=100;?
??????txt.BorderStyle=BorderStyle.Groove;?????
??????if(i==0||j==0)
??????{
???????txt.BackColor=System.Drawing.Color.LightYellow;
???????txt.ReadOnly=false;
????????
??????}
??????else
??????{
???????txt.BackColor=System.Drawing.Color.Silver;
???????txt.ReadOnly=true;
??????}
??????if(i==0&&j==0)
??????{
???????txt.BackColor=System.Drawing.Color.Silver;
???????txt.ReadOnly=true;
??????}
??????
???????????
??????td.Controls.Add(txt);?????
??????tr.Cells.Add(td);????
?????
?????}
?????table1.Controls.Add(tr);
????}
????table1.ID="temptab";
????table1.CellPadding=0;
????table1.CellSpacing=0;
????table1.BorderWidth=1;
????table1.BorderWidth=Unit.Pixel(1);
??????????????? table1.BackColor=System.Drawing.Color.FromName("#d4d0c8");
????table1.BackColor=System.Drawing.Color.FromName("#d4d0c8");
????tab1.Controls.Add(table1);
???}
???
??}
5)取动态控件的值
Request.Form.Get("txt00")
Request.Form.Get("txt11")