locked
Multiple Dynamic TextBox in ASP.NET RRS feed

  • Question

  • User-824693832 posted

    Can you please help me to integrate Dynamic TextBox in ASP.NET and store data to SQL Server using button click. Every time user click on the button will generate 2 individual Textbox and another will store value to database.

    Example:

    [ TextBox1 ]     [TextBox2]

    |New Row|   |Save|

    Thursday, November 9, 2017 5:22 AM

All replies

  • User347430248 posted

    Hi kowserhossai,

    to generate the textbox dynamically you can refer the example below.

    protected void AddTextBox(object sender, EventArgs e)
    {
        int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
        this.CreateTextBox("txtDynamic" + index);
    }
     
    private void CreateTextBox(string id)
    {
        TextBox txt = new TextBox();
        txt.ID = id;
        pnlTextBoxes.Controls.Add(txt);
     
        Literal lt = new Literal();
        lt.Text = "<br />";
        pnlTextBoxes.Controls.Add(lt);
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
        int i = 1;
        foreach (string key in keys)
        {
            this.CreateTextBox("txtDynamic" + i);
            i++;
        }
    }

    below is the code to fetch the values from textbox which you can stored in database.

    protected void GetTextBoxValues(object sender, EventArgs e)
    {
        string message = "";
        foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
        {
            message += textBox.ID + ": " + textBox.Text + "\\n";
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
    }

    for a detailed example refer link below.

    Get Value (Text) of dynamically created TextBox in ASP.Net using C# and VB.Net

    to store the value to database , you can refer example below.

    string cs=System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("INSERTINTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    Regards

    Deepak

    Thursday, November 9, 2017 7:10 AM
  • User-824693832 posted

    Hi Deepak,

    Thanks for the response. This will create Singel TextBox whereas I am looking for 2 Horizontal TextBox and store it to SQL Database with per button click. Can you help me in this case please? 

    So far I am able to generate two horizontal TextBox but unable to store those data in Database.

    Thursday, November 9, 2017 9:40 AM
  • User347430248 posted

    Hi kowserhossai...,

    you had mentioned that,"So far I am able to generate two horizontal TextBox but unable to store those data in Database."

    so here, I will not show you how to create two textboxes dynamically, because you already done it.

    I will show you how to insert value in database.

    first of all you need to create table with  two fields. here I am creating just one field. you can modify the table design as per your requirement.

    then try to connect the database using "Server Explorer".

    then come to the code behind and add namespace below.

    using System.Data.SqlClient;

    then go to the "GetTextBoxValues" Function.

    modify the code like below.

     protected void GetTextBoxValues(object sender, EventArgs e)
    
            {
    
                string message = "";
    
                foreach (TextBox textBox in pnlTextBoxes.Controls.OfType<TextBox>())
    
                {
    
                    message += textBox.ID + ": " + textBox.Text + "\\n";
                    string cs = "Data Source=VDI-V-PADEE;Initial Catalog=demo;Integrated Security=True";
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("INSERT INTO demo_tbl (val) VALUES('" + textBox.Text + "')", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
    
    
                }
    
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);
    
            }
    

    Output:

    so I hope now you are clear that how to use that code to insert data in database.

    further , you just need to modify the code as per your requirement.

    Regards

    Deepak

    Friday, November 10, 2017 1:42 AM
  • User-1001718463 posted

    Hi Deepak,

    Thanks for the response. This will create Singel TextBox whereas I am looking for 2 Horizontal TextBox and store it to SQL Database with per button click. Can you help me in this case please? 

    So far I am able to generate two horizontal TextBox but unable to store those data in Database.

    Hi kowserhossain,

    do you keep the code for generate two horizontal textbox and store it into database? Can you share it to me through email? lysanchen928@gmail.com

    Thursday, November 8, 2018 2:25 PM