如何动态生成页面控件,并取到值?(.net)

博客介绍了在CS中动态生成页面控件并操作的方法。包括在页面添加按钮,在Page_Load中执行CreateTab保证刷新时动态表格不消失,生成动态表格的具体实现,以及取动态控件值的方式,如使用Request.Form.Get。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值