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
				myArrayList.Add(I)
			Next I
			ddlFields.DataSource = myArrayList
			ddlFields.DataBind
			' 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> "
			myPlaceHolder.Controls.Add(lblNumber)
			txtNumber      = New TextBox
			txtNumber.Id   = "number" & I
			txtNumber.Text = I
			myPlaceHolder.Controls.Add(txtNumber)
			lblNumber      = New Label
			lblNumber.Text = "<br />" & vbCrLf
			myPlaceHolder.Controls.Add(lblNumber)
		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
</script>
<html>
<head>
<title>ASP.NET Dynamic Form Fields Sample</title>
</head>
<body>
<form runat="server">
<!-- This section is always shown -->
<p>
How many numbers would you like to enter?
</p>
<!-- 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">
	<p>
	You indicated you wanted to enter <asp:Literal id="litNumbersToEnter" runat="server" /> numbers.  Enter them below:
	</p>
	<asp:PlaceHolder id="myPlaceHolder" runat="server" />
	<asp:Button id="btnSubmitNumbers" runat="server"
		Text    = "Submit Numbers"
		OnClick = "btnSubmitNumbers_Click"
	/>
</asp:Panel>
<asp:Panel id="pnlShowResults" runat="server">
	<p>
	You entered <asp:Literal id="litNumbersEntered" runat="server" /> numbers.
	</p>
	<p>
	They were:
	</p>
	<p>
	<asp:Literal id="litNumberList" runat="server" />
	</p>
</asp:Panel>
</form>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/form_dynamic_aspx.asp">here</a>
to read about and download the source code.
</p>
</body>
</html>