有增加和保存两个按钮,点击增加则textbox增加 即又出现另一个textbox而再点击也是出现一样 而且输入数据保存可以到数据库中

解决方案 »

  1.   


            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["tb"] != null)
                {
                    List<TextBox> list = Session["tb"] as List<TextBox>;
                    foreach (TextBox tb in list)
                        this.form1.Controls.Add(tb);
                }
            }        protected void btnAdd_Click(object sender, EventArgs e)
            {
                List<TextBox> list = new List<TextBox>();            if (Session["tb"] != null)
                    list = Session["tb"] as List<TextBox>;            TextBox tb = new TextBox();
                tb.ID = "TextBox" + (list.Count + 1).ToString();
                tb.Text = tb.ID;            list.Add(tb);
                Session["tb"] = list;
                foreach (TextBox textbox in list)
                    this.form1.Controls.Add(tb);
            }        protected void btnSave_Click(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("连接字符串"))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into table values('" +
                        (this.FindControl("TextBox1") as TextBox).Text + "','" +
                        ……………
                        (this.FindControl("TextBoxn") as TextBox).Text + "')";
                    cmd.ExecuteNonQuery();
                }
            }