Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL
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 23-Mar-13 1:36am
Comments
pryashrma at 23-Mar-13 7:30am
   
from code behind- use form1.Controls.Add()
Rate this: bad
good
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);
        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,
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hi,
<%@ 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:
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;
            }
        }
  Permalink  
Rate this: bad
good
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.Controls.Add(MyTextBox);
     }
           
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 220
2 DamithSL 190
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
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