日期:2014-05-20  浏览次数:20928 次

动态生成TextBox控件和获取控件输入的值?
点击button动态生成TextBox控件和获取控件输入的值?

------解决方案--------------------
#1的一点都不厚道,这么简单的功能还打广告

丢个button到form上面
C# code

    public partial class Form1 : Form
    {
        TextBox _mtextbox = null;
        public Form1()
        {
            InitializeComponent();
            button1.Text = "点击创建控件";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_mtextbox == null)
            {
                _mtextbox = new TextBox();
                this._mtextbox.Location = new System.Drawing.Point(150, 149);
                this._mtextbox.Name = "textBox1";
                this._mtextbox.Size = new System.Drawing.Size(100, 21);
                this._mtextbox.TabIndex = 1;
                this.Controls.Add(_mtextbox);
                button1.Text = "在输入框中输入信息以后点击";
            }
            else
                MessageBox.Show(this._mtextbox.Text);
        }
    }

------解决方案--------------------
C# code

 protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["isok"] != null)
        {
            CreateTextBox();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateTextBox();
    }

    private void CreateTextBox()
    {
        TextBox tb = new TextBox();
        tb.ID = "TextBox1";
        tb.AutoPostBack = true;
        tb.TextChanged += new EventHandler(tb_TextChanged);
        this.form1.Controls.Add(tb);
        ViewState["isok"] = true;
    }

    void tb_TextChanged(object sender, EventArgs e)
    {
        Response.Write((sender as TextBox).Text);
    }