asp.net 动态生成控件并获取其值
                        
                            时间:2021-07-01 10:21:17
                            帮助过:21人阅读
							                        
                     
                    
                    
             代码如下:
//动态生成三个控件 
protected void Button1_Click(object sender, EventArgs e) 
{ 
for (int i = 0; i < 3; i++) 
{ 
TextBox t = new TextBox(); 
t.ID = string.Format("newTextBox{0}",i); 
Panel1.Controls.Add(t); 
} 
ListControlsInPanel(); 
} 
 代码如下:
//获取动态生成的控件的值 
protected void Button3_Click(object sender, EventArgs e) 
{ 
string str=""; 
string[] ak = Request.Form.AllKeys; 
for (int i = 0; i < Request.Form.Count; i++) 
{ 
//只筛选出动态生成的三个控件的值 
if(ak[i].IndexOf ("new")>-1) 
str += string.Format("<li>{0}</li><br>",Request .Form [i]); 
} 
Label1.Text = str; 
} 
//这里新生成的控件的ID命名使用new开头,同时应保证页面没有控件的ID包含new。