3
Answers

dynamically adding textboxes

suchi rana

suchi rana

13y
2k
1
Hi,

My requriment is i want to give dinamicaly adding controls in my web page . that means if i click add button dinamicallly add textboxs in my page. like wise how many times if i click add buton such textboxs will apper. how to do.


Thanks in Advance
Answers (3)
0
suchi rana

suchi rana

NA 18 18.7k 13y
Hi , Thanks for your response
0
Hemant Kumar

Hemant Kumar

NA 3k 215k 13y
Hi please refer this link
http://geekswithblogs.net/dotNETvinz/archive/2009/03/17/dynamically-adding-textbox-control-to-aspnet-table.aspx
http://forums.asp.net/t/1152363.aspx
0
Satyapriya Nayak

Satyapriya Nayak

NA 53k 8m 13y
Hi Suchi,

Try this...


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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:textbox runat="Server" id="txtTBCount" Columns="3" />

    <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtTBCount"
           MinimumValue="1" MaximumValue="10" Type="Integer"
           ErrorMessage="Choose a value between 1 and 10" />

    <br />
    <asp:button ID="Button1" runat="server" Text="Create Dynamic TextBoxes"
           OnClick="CreateTextBoxes" />

    <p>
    <asp:PlaceHolder runat="server" id="TextBoxesHere" />
 </p>
    </div>
    </form>
</body>
</html>



using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
   
    protected void CreateTextBoxes(object sender, EventArgs e)
    {
        CreateTextBoxes();
    }
    int count = 1;

    void IterateThroughChildren(Control parent)
{
  foreach (Control c in parent.Controls)
  {
    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
    {
          ((TextBox) c).Text = "TextBox " + count.ToString();
          ((TextBox) c).Columns = 10;
          count++;
    }
       
    if (c.Controls.Count > 0)
    {         
      IterateThroughChildren(c);         
    }
  }
}
    void CreateTextBoxes()
    {
        if (!Page.IsValid) return;

        int n = Int32.Parse(txtTBCount.Text);

        // now, create n TextBoxes, adding them to the PlaceHolder TextBoxesHere
        for (int i = 0; i < n; i++)
        {
            TextBoxesHere.Controls.Add(new TextBox());
        }

        // now, set the Text property of each TextBox
        IterateThroughChildren(this);
    }


}




Thanks