<script runat="server" language="C#">
int count = 1;
void IterateThroughChildren(Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") &&
c.ID == null)
{
((TextBox) c).Text = "TextBox " + count.ToString();
((TextBox) c).Columns = 10;
count++;
}
if (c.Controls.Count > 0)
{
IterateThroughChildren(c);
}
}
}
void CreateTextBoxes(Object sender, EventArgs e)
{
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);
}
</script>
<form runat="server">
How many TextBoxes would you like to create? (<i>Please choose vaule between 1 and 10</i>)<br />
<asp:textbox runat="Server" id="txtTBCount" Columns="3" />
<asp:RangeValidator runat="server" ControlToValidate="txtTBCount"
MinimumValue="1" MaximumValue="10" Type="Integer"
ErrorMessage="Make sure that you choose a value between 1 and 10!" />
<br />
<asp:button runat="server" Text="Create Dynamic TextBoxes"
OnClick="CreateTextBoxes" />
<p>
<asp:PlaceHolder runat="server" id="TextBoxesHere" />
</form>
|