动态添加文本框并在ASP.NET中的文本框中保存数据 [英] Dynamically adding textbox and saving data in textbox in ASP.NET

查看:76
本文介绍了动态添加文本框并在ASP.NET中的文本框中保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i m trying to add textbox dynamically at runtime and saving data in sql server. i am able generate textbox but not able send data in database and it is not giving any error also. what i am doing wrong? here is my code

What I have tried:

protected void btnAddCtrl_Click(object sender, EventArgs e)
{
    int rowCount = 0;

    //initialize a session.
    rowCount = Convert.ToInt32(Session["clicks"]);

    rowCount++;

    //In each button clic save the numbers into the session.
    Session["clicks"] = rowCount;


    //Create the textboxes and labels each time the button is clicked.
    for (int i = 0; i < rowCount; i++)
    {

        TextBox TxtBoxU = new TextBox();

        //TextBox TxtBoxE = new TextBox();

        Label lblU = new Label();
       // Label lblE = new Label();

        TxtBoxU.ID = "TextBoxU" + i.ToString();
      //  TxtBoxE.ID = "TextBoxE" + i.ToString();

        lblU.ID = "LabelU" + i.ToString();
       // lblE.ID = "LabelE" + i.ToString();


        lblU.Text = "User " + (i + 1).ToString() + " : ";
       // lblE.Text = "E-Mail : ";

        //Add the labels and textboxes to the Panel.
        Panel1.Controls.Add(lblU);
        Panel1.Controls.Add(TxtBoxU);
        Panel1.Controls.Add(new LiteralControl("<br />"));
        Panel1.Controls.Add(new LiteralControl("<br />"));




    }


}


protected void btnSave_Click(object sender, EventArgs e)
{
    foreach (TextBox textBox in Panel1.Controls.OfType<textbox>())
    {
        string constr = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO AssetsDetail (FieldName) VALUES(@FieldName)",con))
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@FieldName", textBox.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }
    }
}</textbox>

推荐答案

试试这个。

受到 ASP.Net的启发持久动态控件:动态控件在ASP.Net中的PostBack后消失 [ ^ ]



try this.
inspired from ASP.Net Persist Dynamic Controls: Dynamic Controls disappear after PostBack in ASP.Net[^]

public partial class WebForm1 : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {


       }

       protected void btnAddCtrl_Click(object sender, EventArgs e)
       {
           int rowCount = 0;
           rowCount = Convert.ToInt32(Session["clicks"]);
           rowCount++;
           Session["clicks"] = rowCount;
           int i = rowCount;

           TextBox TxtBoxU = new TextBox();
           Label lblU = new Label();
           TxtBoxU.ID = "TextBoxU" + i.ToString();
           lblU.ID = "LabelU" + i.ToString();
           lblU.Text = "User " + i.ToString() + " : ";
           Panel1.Controls.Add(lblU);
           Panel1.Controls.Add(TxtBoxU);


       }

       protected void Page_PreInit(object sender, EventArgs e)
       {
           List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("TextBoxU")).ToList();
           foreach (string key in keys)
           {
               string id = key.Replace("TextBoxU", "");
               this.CreateTextBox(key, "LabelU" + id ,   id);

           }
       }

       private void CreateTextBox(string id, string labelId,string idNo )
       {

           Label lbl = new Label();
           TextBox txt = new TextBox();
           txt.ID = id;
           lbl.ID = labelId;
           lbl.Text = "User " + idNo + " : ";
           Panel1.Controls.Add(lbl);
           Panel1.Controls.Add(txt);

       }

       protected void btnSave_Click(object sender, EventArgs e)
       {

           foreach (TextBox textBox in Panel1.Controls.OfType<TextBox>())
           {
               string text = textBox.Text;
               // your code to insert into database
           }
       }
   }


这篇关于动态添加文本框并在ASP.NET中的文本框中保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆