You must Sign In to post a response.
  • Category: ASP.NET

    How to Add text Box dynamically and insert into db

    Hello Developers ,

    I want to Add TextBox dynamically at runtime and store that textboxes values into database table.
    I am tried lot of times but i didn't get exactly output.
    So could anyone know please help me to i am done this task

    Thanks with,
    PAul.S
  • #763339
    You can do it using javascript. If you want you can generate this script string from the server side using your database.

    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="PartSearchTable">
    <table>

    <script>
    var tC = new Array(); //TableContent
    tC.push('<tr>');
    tC.push('<td>');
    tC.push(' <input name="TxtName" type="text" maxlength="5" id="TxtName" onchange="CalculateTotal();" />');
    tC.push('</td>');
    tC.push('</tr>');

    var html = '';
    for (var i = 0; i < tC.length; i++) {
    html += tC[i];
    }
    $('#PartSearchTable').append(html);
    </script>

    By Nathan
    Direction is important than speed

  • #763340
    There are multiple ways to create dynamic textbox, you can check below snippet
    //create one panel
    Panel pn1 = new Panel();
    pn1.ID = "pnlTextBox";
    pn1.Width = 200;
    this.form1.Controls.Add(pn1);

    //create a textbox control and add it in panel
    Textbox txtNew = new Textbox();
    txtNew.ID = "ID1";
    txtNew.Runat = "Server";
    pn1.controls.add(txtNew);

    //access control values at runtime
    if you want to access textbox at runtime you can use Request object
    string szValue = Request.Form["txtNew"] ;

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763348

    Hello Paul Raj,

    You can easily create dynamic control and get their values by using function FindControl.

    Refer the below code :

    HTML Mark up :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    <asp:Button ID="ButtonSave" runat="server" Text="Save"
    onclick="ButtonSave_Click" />
    </div>
    </form>
    </body>
    </html>

    Code - behind :

    using System.Data;
    using System.Data.SqlClient;

    protected void Page_Load(object sender, EventArgs e)
    {
    Label lbl = new Label();
    lbl.ID = "Label1";
    lbl.Text = "Name :";
    lbl.Attributes.Add("runat", "server");
    Panel1.Controls.Add(lbl);

    TextBox txtbox = new TextBox();
    txtbox.ID = "TextBox1";
    txtbox.Attributes.Add("runat","server");
    Panel1.Controls.Add(txtbox);
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
    try
    {
    TextBox txt = Panel1.FindControl("TextBox1") as TextBox;
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True");
    SqlCommand cmd = new SqlCommand("INSERT INTO BabyNames(Name) VALUES ('" + txt.Text.Trim() + "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    Response.Write("Name saved successfully.");
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    }

    See the attached image. It's an output i have got using above code.

    Hope this is what you are looking for.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #763383
    Your code is nice Mr.lalan but my requirement is Aspx design ,add a button (Ex:btn name =ADD)near right side after the textbox and same column.

    if i click the ADD button then only dynamic textbox created
    for example when i am 1st time click ex:textboxname="Value1", ithen i click 2nd time also a textbox should be created textboxname="Value2"

    if i click the add button 10 times 10 text box should
    be created left side to right Mr.Lalan ;
    and also store the dynamic text box value into databse Lalan.

    if you are also know my requirement
    give me some
    Please help me Mr.Laalan

    Thanks with,
    Paul.S


Sign In to post your comments