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,
  • #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">

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

    var html = '';
    for (var i = 0; i < tC.length; i++) {
    html += tC[i];

    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;

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

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

    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="" >
    <head runat="server">
    <form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server">
    <asp:Button ID="ButtonSave" runat="server" Text="Save"
    onclick="ButtonSave_Click" />

    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");

    TextBox txtbox = new TextBox();
    txtbox.ID = "TextBox1";

    protected void ButtonSave_Click(object sender, EventArgs e)
    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);
    Response.Write("Name saved successfully.");
    catch (Exception ex)

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

    Hope this is what you are looking for.

    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,

Sign In to post your comments