提问人:fateh 提问时间:2/1/2023 更新时间:2/4/2023 访问量:85
在 C# 中使用字符串作为控件 ID ASP.NET
using string as control id in C# ASP.NET
问:
我有一个包含 2 个文本框项目和一个按钮的页面 TextBox1 包含一个单词,而 TextBox2 为空
现在我想把TextBox1.Text的内容放在TextBox2.Text中,点击按钮,
我试过了:
protected void Button1_Click(object sender, EventArgs e)
{ Page.FindControl("TextBox2").Text = TextBox1.Text; }
这段代码不起作用,如何让它起作用?
答:
1赞
Naser Hamdan
2/2/2023
#1
TextBox textbox2= (TextBox)FindControlRecursive(Page, "TextBox2");
尝试使用它,参考这篇文章
0赞
mo fa
2/4/2023
#2
您需要先定义它,然后应用属性
protected void Button1_Click(object sender, EventArgs e)
{
TextBox textBox2 = (TextBox)Page.FindControl("TextBox2");
textBox2.Text = TextBox1.Text;
}
评论
FindControl
不会递归搜索容器,因此它可能不是 Page 的直接子级。为什么不只是 ?TextBox2.Text = TextBox1.Text;