using string as control id in C# ASP.NET

119 Views Asked by At

i have a page with 2 textbox items and a button textbox1 contains a word , and textbox2 is empty

now i want to put content of TextBox1.Text in TextBox2.Text with button click,

i tried:

protected void Button1_Click(object sender, EventArgs e) 
{ Page.FindControl("TextBox2").Text = TextBox1.Text; }

this code don't work ,how to make this work?

2

There are 2 best solutions below

0
mo fa On BEST ANSWER

you need to define it first then apply properties

protected void Button1_Click(object sender, EventArgs e) 
{ 
    TextBox textBox2 = (TextBox)Page.FindControl("TextBox2");
    textBox2.Text = TextBox1.Text; 
}
0
Naser Hamdan On
TextBox textbox2= (TextBox)FindControlRecursive(Page, "TextBox2");

try using this, referencing this article