How To Create TextBox Control Dynamically at Runtime

How To Create TextBox Control Dynamically at Runtime


In this post, I will show how to create Textbox Control dynamically and read their 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();
    }
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru