4-3 Dynamically add controls in the web page life cycle

Press the Button to create a TextBox control:

The background code is written in the Button_Click event (the background code can also be unloaded in the PageLoad event):

protected void Button1_Click(object sender, EventArgs e)

    {
        int i = 0;
        try { i = Int16.Parse(txt_num.Text); }
        catch { }
        
        TextBox _txt;
        if (i > 0)
        {
            for (int _i = 0; _i < i; _i++)
            {
                _txt = new TextBox();
                _txt.Text = _i.ToString();
                pnl1.Controls.Add(_txt);
            }
        }

    }


Dynamically created controls in PageLoad can retain operational state while static controls cannot

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325903916&siteId=291194637