How To Create TextBox Control Dynamically at Runtime

http://aspdotnetcodebook.blogspot.com/2008/03/how-to-create-textbox-control.html in this post i will show how to create Textbox Control dynamically and read there value.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBoxDynamic.aspx.cs" Inherits="TextBoxDynamic" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <table width="100%" cellpadding="0" cellspacing="0">
               <tr>
                   <td colspan="2">
                       <asp:PlaceHolder ID="phTextBoxes" runat="Server"></asp:PlaceHolder>
                   </td>
               </tr>
               <tr>
                   <td>
                       <asp:Button ID="btnAddTitle" runat="Server" Text="Add" />
                   </td>
                   <td>
                       <asp:Button ID="btnRead" runat="Server" Text="Read" OnClick="btnRead_Click" />
                   </td>
               </tr>
           </table>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TextBoxDynamic : System.Web.UI.Page
{
   
    string strValue = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void CreateTextBoxes()
    {

        for (int counter = 0; counter <= NumberOfControls; counter++)
        {
            TextBox tb = new TextBox();
            tb.Width = 150;
            tb.Height = 18;
            tb.TextMode = TextBoxMode.SingleLine;
            tb.ID = "TextBoxID" + (counter + 1).ToString();
            // add some dummy data to textboxes
            tb.Text = "Enter Title " + counter;
            phTextBoxes.Controls.Add(tb);
            phTextBoxes.Controls.Add(new LiteralControl("<br/>"));

        }

    }
    protected override void CreateChildControls()
    {
        // Here we are recreating controls to persist the ViewState on every post back
        if (Page.IsPostBack)
        {
            NumberOfControls += 1;
            CreateTextBoxes();
        }
        else
        {
            CreateTextBoxes();
            // Increase the control value to 1
            NumberOfControls = 0;
        }

    }
    protected void btnAddTitle_Click(object sender, EventArgs e)
    {


        NumberOfControls += 1;


    }
    public int NumberOfControls
    {
        get
        {
            if (ViewState["Count"] == null)
            {
                return 0;
            }
            return (int)ViewState["Count"];
        }
        set
        {
            ViewState["Count"] = value++;
        }
    }

    private void ReadTextBoxes()
    {
        strValue = string.Empty;
        int n = NumberOfControls;

        for (int i = 0; i <= NumberOfControls; i++)
        {

            string boxName = "TextBoxID" + (i + 1).ToString();
            TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
            strValue += tb.Text + "\n";
          



        }
        Response.Write(strValue);


    }


    protected void btnRead_Click(object sender, EventArgs e)
    {
        ReadTextBoxes();


    }
}

15 comments:

  1. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it. Thanks

    ReplyDelete
  2. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it. Thanks

    ReplyDelete
  3. Please disregard my previous comment regarding a fix to Read button. I figure out how to fix it. Actually check to tell the source of button click. Don't increase NumberOfControls if button click is from Read. thanks

    ReplyDelete
  4. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it.Please tell me

    ReplyDelete
  5. Hello Sir, I'm a new user of .Net. I need ur help to create dynamic textboxes by getting the count of the selected item from the dropdownlist using query n display only those many text boxes. but the text box must display only after selecting the item from dropdownlist. Plz help me in this... i'll be waiting for ur reply... thank you.

    ReplyDelete
  6. Happy to have found the two articles on dynamic textboxes; I've been struggling with it for days.

    Having a small problem because mye table is also being created dynamically, and can't find the right syntax to add the placeholder to the dynamic table.

    If anyone has a suggestion, I'd appreciate it.

    ReplyDelete
  7. This code is working well but one more textbox is added when i am clicking read button. I think your solution is for clicking any button in a form. any idea to clear the problem rectify the problem that is extra textbox is add

    ReplyDelete
  8. hiiiii
    i am jitendra but i want to say that the last value is appear bydefault when i click on read button

    ReplyDelete
  9. Hi ................
    Till TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
    i am getting texbox id but textbox value is showing null please anybody help me

    ReplyDelete
  10. I FRIENDS THIS IS KAREM,

    I HAVE ONE DOUBT WHAT IS USE OF viewState IN ABOVE EXAMPLE CODE ,COULD ANY ONE OF YOU HELP ME TO CLARIFY THAT ONE
    WE CREATING TEXT BOXES AND ACCESS THOSE VALUE IN SIDE THE TEXTBOXES

    ReplyDelete
  11. Nice Code... Can you please tell me why one more text box is created when i am clicking Add button

    ReplyDelete
  12. Am struggling with this for two daya.. Thanks it helped me a lot..

    ReplyDelete
  13. Nice code but add one more text box when we read values........please any one solve this .......
    thnx

    ReplyDelete