c#动态得到控件名并更改其内容

来源:百度知道 编辑:UC知道 时间:2024/05/29 16:03:39
通过一个参数得到控件的id ,然后进行更改
如 有以下控件
news1
news2
news3
...

得到参数id的值为"2" 通过拼合 "news"+id,得到控件名为news2
现在如何改news2的Text为 1234
这个页面引用了母版页,但问题所说控件在内容页中,现在发现用FindControl都找不到内容页的控件,都为null,不知道找内容页的FindControl要怎么写

FindControl只能在直接父容器调用时,才能找到。
最好在你要找的这些control外加一个panel控件,然后调用这个panel的FindControl

protected void Button1_Click(object sender, EventArgs e)
{
SetTextBox(txtaa.Text.ToString());//传递参数
}
public void SetTextBox(string txtID)
{
switch (txtID)
{
case "1":
news1.Text = "1";
break;
case "2":
news2.Text = "2";
break;
case "3":
news3.Text = "3";
break;

}
}

int Id = 1;
Control control = this.FindControl("news" + Id);
TextBox textBox1 = control as TextBox;
if(textBox1 != null)
{
textBox1.Text = "aaaaaaaaaa";
}

for(int id=0;;id++