Click here to Skip to main content
13,093,184 members (81,960 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I Have a text box in my aspx page, now i want to display textboxes depending on the number what i given to the designed text box
that is
If i give 5 in my textbox I want to display 5 textboxs also i want to get the displayed textbox values which i gave..
Posted 23-Mar-13 0:36am
pryashrma 23-Mar-13 7:30am
from code behind- use form1.Controls.Add()
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hello Arunadevi,

You can do this pretty easily using JavaScript. Look at following code sample.
<script type="text/javascript">
function addTextBoxes(total, parentName) {
    var elDiv = $('parentName');
    for (var tmpId = 0; tmpId < total; tmpId++) {
        var fldVal = $('<input type="text"/>');
        fldVal.attr('id', 't_' + tmplId);
        fldVal.attr('name', 't_' + tmplId);
function readValues(total) {
    var vals = new Array();
    for (var i = 0; i < total; I++) {
        var fld = document.gtElementById('t_' + i);
        vals[I] = fld.value;
    return vals;

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:Label ID="lblQty" Text="Enter Quantity" runat="Server"></asp:Label>
        <asp:TextBox ID="txtCreateTextBoxQty" runat="server"></asp:TextBox>
        <asp:Label ID="lblDefaultVal" Text="Enter Default Value" runat="Server"></asp:Label>
        <asp:TextBox ID="txtDefaultValue" runat="server"></asp:TextBox>
        <asp:Button ID="btnCreateControls" runat="server" Text="Add" OnClick="btnCreateControls_Click" />
        <asp:PlaceHolder ID="phControls" runat="server"></asp:PlaceHolder>

protected void btnCreateControls_Click(object sender, EventArgs e)
                //Holds the quantity of textboxes to be created
                int intTextQty = int.Parse(txtCreateTextBoxQty.Text);
                //Assigns default values to the textboxes created
                string strDefaultValue = txtDefaultValue.Text;
                for (int i = 0; i < intTextQty; i++)
                    TextBox txt = new TextBox();
                    txt.ID = "txt" + i;
                    txt.Text = strDefaultValue;
                    phControls.Controls.Add(new LiteralControl("<br />"));
                    phControls.Controls.Add(new LiteralControl("<br />"));
            catch (Exception)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

//Call this method at the button_click even or text_change event. (Whatever you like)
public void CreateTextBoxes()
     //Number of text boxes to be created
     int numTxts = Convert.ToInt16(txtCreateTextBoxes.Text);
     for (int i = 0; i < numTxts; i++)
          TextBox myTextBox = new myTextBox();
          myTextBox.ID = "TextBox" + i;
          myTextBox.Width = 540;
          myTextBox.Height = 60;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100