(论坛答疑点滴)怎么给Table动态添加控件并且得到控件的值?

博客介绍了给Table动态添加控件并获取其值的方法。通过代码展示了如何在点击按钮时添加文本框和下拉列表等控件到Table中,还实现了添加按钮并在点击该按钮时获取Table中各控件的值,同时处理页面加载时的控件恢复。
此例子达到的效果是:
每按一次Button1,在表格Table1中添加一行(行中有2列,一列是文本框,一列是下拉框),并且当按钮第一次按下时再添加一个按钮,点击这个动态添加的按钮,输出表格中所有的控件的值。

前台:
None.gif<form id="Form1" method="post" runat="server">
None.gif            
<asp:Table id="Table1" runat="server"></asp:Table>
None.gif            
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder><BR><BR>
None.gif            
<asp:Button id="Button1" runat="server" Text="添加一行"></asp:Button>
None.gif        
</form>

放置一个Table用来动态添加控件,放置一个PlaceHolder用来动态添加按钮,按下这个按钮得到表中控件的值,按下Button1按钮一次就添加一行。

后台:

Button1按钮的事件:
None.gifprivate void Button1_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            AddTextBoxs();
InBlock.gif            
if(ViewState["Count"]==null)AddButton();
InBlock.gif            ViewState[
"Count"]=Convert.ToInt16(ViewState["Count"])+1;
ExpandedBlockEnd.gif        }

两个方法:一个用来动态添加表格中的行,一个用来动态添加按钮(按钮不是按下Button1添加一次的,所以加上if(ViewState["Count"]==null)表示只有第一次加载按下按钮的时候才添加)
None.gifprivate void AddTextBoxs()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            TableRow tr
=new TableRow();
InBlock.gif            TableCell tc1
=new TableCell();
InBlock.gif            TextBox t
=new TextBox();
InBlock.gif            t.ID
="tb"+Table1.Rows.Count;
InBlock.gif            tc1.Controls.Add(t);
InBlock.gif            TableCell tc2
=new TableCell();
InBlock.gif            DropDownList dpl
=new DropDownList();
InBlock.gif            dpl.ID
="dpl"+Table1.Rows.Count;
InBlock.gif            
for(int i=0;i<10;i++)dpl.Items.Add(i.ToString());
InBlock.gif            tc2.Controls.Add(dpl);
InBlock.gif            tr.Cells.Add(tc1);
InBlock.gif            tr.Cells.Add(tc2);
InBlock.gif            Table1.Rows.Add(tr);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private void AddButton()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{        
InBlock.gif            Button b
=new Button();
InBlock.gif            b.ID
="btn";
InBlock.gif            b.Text
="按钮";
InBlock.gif            b.Click 
+= new System.EventHandler(btn_Click);
InBlock.gif            PlaceHolder1.Controls.Add(b);
ExpandedBlockEnd.gif        }

最后是那个动态添加的按钮的事件:
None.gifprivate void btn_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
for(int i=0;i<Table1.Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Response.Write(((TextBox)Table1.Rows[i].FindControl(
"tb"+i)).Text+((DropDownList)Table1.Rows[i].FindControl("dpl"+i)).SelectedValue+"<br>");
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

其实动态添加的控件不复杂,只需要注意一点:动态添加的控件在PostBack的时候也需要再次添加,那么怎么知道是不是按下了按钮,或者说怎么知道已经按了几次按钮?就用一个标示位存放在ViewState中即可。

Page_Load事件:
None.gifprivate void Page_Load(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if(ViewState["Count"]!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for(int i=0;i<Convert.ToInt16(ViewState["Count"]);i++)
InBlock.gif                    AddTextBoxs();
InBlock.gif                AddButton();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
注意不要添加if(!IsPostBack){},相反你倒可以添加if(IsPostBack),因为页面第一次加载不可能已经按下按钮了。

转载于:https://www.cnblogs.com/lovecherry/archive/2005/04/16/138968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值