ASP 101 - Active Server Pages 101 form_dynamic.aspx
<%@ Page Language="VB" %>
<script language="VB" runat="server">
	Sub Page_Load(sender As Object, e As EventArgs)
		' Init Page on first load.
		If Not Page.IsPostBack Then
			' Only need to populate DropDownList the first time through.
			Dim I As Integer
			Dim myArrayList As New ArrayList(20)
			For I = 1 to 20
			Next I
			ddlFields.DataSource = myArrayList
			' Hide Panels
			pnlEnterNumbers.Visible = False
			pnlShowResults.Visible  = False
		End If
	End Sub
	Sub btnEnterNumbers_Click(sender As Object, e As EventArgs)
		Dim I              As Integer
		Dim intNumberCount As Integer
		' Get number of items to be entered.
		intNumberCount = ddlFields.SelectedItem.Value
		' Set label text to reflect the number.
		litNumbersToEnter.Text = intNumberCount
		' Add one textbox to enter each item.
		Dim lblNumber As Label
		Dim txtNumber As TextBox
		For I = 1 To intNumberCount
			lblNumber      = New Label
			lblNumber.Text = "<strong>Number " & I & ":</strong> "
			txtNumber      = New TextBox
			txtNumber.Id   = "number" & I
			txtNumber.Text = I
			lblNumber      = New Label
			lblNumber.Text = "<br />" & vbCrLf
		Next I
		' Add number of items to viewstate
		ViewState("NumberCount") = I - 1
		' Show and hide the appropriate panels
		pnlEnterNumbers.Visible = True
		pnlShowResults.Visible  = False
	End Sub
	Sub btnSubmitNumbers_Click(sender As Object, e As EventArgs)
		Dim I               As Integer
		Dim intNumberCount  As Integer
		Dim myStringBuilder As StringBuilder
		' Determine number of items to be retrieved
		intNumberCount = ViewState("NumberCount")
		' Set label text to reflect the number.
		litNumbersEntered.Text = intNumberCount
		' In the real world you'd validate the entries and then do
		' whatever you're doing to process the results here.  I'm
		' just echoing them back out to the browser.
		' Use a string builder to build the results to be displayed.
		myStringBuilder = New StringBuilder()
		For I = 1 To intNumberCount
			myStringBuilder.Append("<strong>Number " & I & ":</strong> ")
			myStringBuilder.Append(Request.Form("number" & I))
			myStringBuilder.Append("<br />" & vbCrLf)
		Next I
		' Show the results
		litNumberList.Text = myStringBuilder.ToString
		' Show and hide the appropriate panels
		pnlEnterNumbers.Visible = False
		pnlShowResults.Visible  = True
	End Sub
<title>ASP.NET Dynamic Form Fields Sample</title>
<form runat="server">
<!-- This section is always shown -->
How many numbers would you like to enter?
<!-- There's no reason this has to be a select box.  You could just as
easily let the user type in a number, but I didn't want to deal with
the additional validation.  Your situation will probably determine if
the wider range of choices is worth the validation work.
<asp:DropDownList id="ddlFields" runat="server" />
<asp:Button id="btnEnterNumbers" runat="server"
	Text    = "Enter Numbers Now"
	OnClick = "btnEnterNumbers_Click"
<!-- This section changes based on what pass through the script we're on. -->
<asp:Panel id="pnlEnterNumbers" runat="server">
	You indicated you wanted to enter <asp:Literal id="litNumbersToEnter" runat="server" /> numbers.  Enter them below:
	<asp:PlaceHolder id="myPlaceHolder" runat="server" />
	<asp:Button id="btnSubmitNumbers" runat="server"
		Text    = "Submit Numbers"
		OnClick = "btnSubmitNumbers_Click"
<asp:Panel id="pnlShowResults" runat="server">
	You entered <asp:Literal id="litNumbersEntered" runat="server" /> numbers.
	They were:
	<asp:Literal id="litNumberList" runat="server" />
<hr />
Click <a href="">here</a>
to read about and download the source code.