create a text box dynamically and get its value

advertisements
TextBox t1 = new TextBox();
t1.Text="123";
qwe2.controls.AddControl(t1);

I added textbox dynamically to qwe2 qwe2 is just a panel

<asp:panel ID="qwe2" runat="server"></asp:panel>

and then on button_submit a have a function

public void button_click(object sender, EventArgs e)
{ var x = t1.Text; }

but t1.text is empty why so? and how to get the value;


You have to add your textbox on all postback when you add control dynamically

    public void page_load(object sender, EventArgs e)
    {       

        if(!ispostback)
        {
        }

        TextBox t1 = new TextBox();t1.Text="123";
        t1.ID ="txtDynamic";
        qwe2.controls.AddControl(t1);
    }

    public void button_click(object sender, EventArgs e)
    {
         TextBox t1 = (TextBox)qwe2.FindControl("txtDynamic");
         var x = t1.Text
    }