如题所示,能在服务器端实现吗?

解决方案 »

  1.   

    protected void Page_Load(object sender, EventArgs e)
        {
            TextBox txtbx= null;    
            DropDownList ddl = null;        for (int i = 0; i < 4; i++)
            {               
                txtbx= new TextBox();
                txtbx.ID = "mytxt" + i; 
                txtbx.Text = "mytxt" + i;                        pnlButton.Controls.Add(txtbx);                ddl= new DropDownList();
                ddl.ID = "mydropdown " + j;
                ddl.Text = "mydropdown " + j;
                ddl.Items.Add("Hii");
                ddl.Items.Add("Hello");
                ddl.AutoPostBack = true;
                ddl.SelectedIndexChanged += new EventHandler(ddl_Click);            pnlButton.Controls.Add(ddl);            Literal lit = new Literal();
                lit.Text = "</br></br>";
                pnlButton.Controls.Add(lit);
            }
        }
      

  2.   

    参考下面四篇,如果还不会,再继续讨论:
    http://www.cnblogs.com/insus/archive/2012/09/24/2700658.html
    http://www.cnblogs.com/insus/archive/2012/09/23/2698613.html
    http://www.cnblogs.com/insus/p/3148345.html
    http://www.cnblogs.com/insus/archive/2011/12/01/2270455.html
      

  3.   

    版主这是是可以动态添加控件,TextBox的值怎么取,
    点击动态控件Button后提交到服务器端后, pnlButton .controls 的findcontrol方法不管用,因为此时,pnlButoon的controls集合里已经没有了这些添加过的TextBox。
      

  4.   

    版主这是是可以动态添加控件,TextBox的值怎么取, 点击动态控件Button后提交到服务器端后, pnlButton .controls 的findcontrol方法不管用,因为此时,pnlButoon的controls集合里已经没有了这些添加过的TextBox。 
      

  5.   

    添加过的你可以使用一个集合保存在ViewState中试试
      

  6.   

    按照你的方法,每次获取TextBox的Text属性时,PlaceHolder控件中的TextBox都被清空了,取不了Text值,不知道问题出在哪,求解答
      

  7.   

    按照你的方法,遍历PlaceHolder控件时,PlaceHolder中的TextBox都被清空了,取不了Text值,不知道问题出在哪,求解答 
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page {
        protected Button btnCreate = new Button();
        protected Button btnRead = new Button();
        protected Label lblMessage = new Label();    protected List<string> ControlsId {
            get {
                if (ViewState["ControlsId"] == null) {
                    ViewState["ControlsId"] = new List<string>();
                    }
                return (List<string>)ViewState["ControlsId"];
                }
            set { this.ViewState["ControlsId"] = value; }
            }    protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);        btnCreate.Text = "在运行时刻添加一个按钮";
            btnCreate.Click += btnCreate_Click;
            this.form1.Controls.Add(btnCreate);
            this.form1.Controls.Add(lblMessage);
            btnRead.Text = "读取文本";
            btnRead.Click += btnRead_Click;
            this.form1.Controls.Add(btnRead);
            
            foreach (string _id in ControlsId) {
                TextBox _ctl = new TextBox();
                _ctl.ID = _id;
                this.form1.Controls.Add(_ctl);
                }
            }    void btnCreate_Click(object sender, EventArgs e) {
            string _id = Guid.NewGuid().ToString();
            ControlsId.Add(_id);
            TextBox _ctl = new TextBox();
            _ctl.ID = _id;
            this.form1.Controls.Add(_ctl);
            }    void btnRead_Click(object sender, EventArgs e) {
            lblMessage.Text = "";
            foreach (string _id in ControlsId) {
                lblMessage.Text += ((TextBox)this.FindControl(_id)).Text;
                }
            }
        
        }