0

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?

fateh
  • 7
  • 3
  • `FindControl` does not recursively search containers so maybe it is not a direct child of Page. Why not just `TextBox2.Text = TextBox1.Text;` ? – Crowcoder Feb 01 '23 at 02:03
  • real use case is more complicated and will take long lines to describe , is there any other valid method ? , in windows forms i used to do: this.Controls.Find("textBox2", true)[0].Text = textBox1.Text; , but this don't work in asp.net – fateh Feb 01 '23 at 02:06

2 Answers2

1
TextBox textbox2= (TextBox)FindControlRecursive(Page, "TextBox2");

try using this, referencing this article

0

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; 
}
mo fa
  • 33
  • 4