Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
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
Comments
pryashrma 23-Mar-13 7:30am    
from code behind- use form1.Controls.Add()

Hello Arunadevi,

You can do this pretty easily using JavaScript. Look at following code sample.
HTML
<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);
        fldVal.appendTo(elDiv);
    }
}
</script>
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;
}

Regards,
 
Share this answer
 
v2
hi,
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!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>
        <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>
    </div>
    </form>
</body>
</html>


Code:
C#
protected void btnCreateControls_Click(object sender, EventArgs e)
        {
            try
            {
                //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(txt);
                    phControls.Controls.Add(new LiteralControl("<br />"));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 
Share this answer
 
C#
//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.Controls.Add(MyTextBox);
     }
           
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900