Search code examples
c#asp.netfindcontrol

using string as control id in C# ASP.NET


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?


Solution

  • 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; 
    }