首页 > 开发 > .Net > 正文

asp.net 动态生成控件并获取其值

2019-10-27 13:21:29
字体:
来源:转载
供稿:网友

复制代码 代码如下:


//动态生成三个控件
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。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表