4-3 网页生命周期 动态添加控件

按下Button,创建TextBox控件:

后台代码写在Button_Click事件中(后台代码也可卸载PageLoad事件中):

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

    }


在PageLoad中动态创建的控件可以保留操作状态而静态控件不可以

猜你喜欢

转载自blog.csdn.net/weixin_38887666/article/details/80163737
今日推荐