Microsoft KB Archive/163499

= How To Use Active Server Pages to Create a Dynamically Growing Form =

Article ID: 163499

Article Last Modified on 5/2/2006

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q163499



SUMMARY
This article provides an example of how to simulate a dynamically growing form using Active Server Pages. This example allows you to enter items one at a time, and add them to a list. Once the list has been created, it can be submitted as a whole.



MORE INFORMATION
In the following example, a basic form is drawn with one text box. You can enter information in that text box, and click Add Item to List. This button submits the form to the same file. As each item is entered, it is added to the form as a new text box with the name ITEMS. Ultimately when the whole list is submitted, there are several items in the list that can be looped through as a collection of ITEMS.

To test this technique, save the text below to a file named Dynaform.asp. Be sure that this file is located in an IIS Virtual Root.

File: DYNAFORM.ASP
<%@ language = vbscript%> <% Response.Expires = 0 %>   Dynamically Growing Form   <%  If Request("Action") = "Submit the List" Then

' Show what was entered. Response.Write "Here are the Items submitted:" nItems = Request.Form("Items").Count For i = 1 To nItems ' Show submitted Items Response.Write Request.Form("Items")(i) & "" Next Response.Write Request("Item") & ""

Else

' Create the form from all items. %>     Items:</B><BR> <%    nItems = Request.Form("Items").Count For i = 1 To nItems ' Show previously submitted Items Response.Write "<INPUT Type=Text Name=Items Value=""" & _ Trim(Request.Form("Items")(i)) & """><BR>" Next

If Request.Form("Item") <> "" Then ' paint a new input box, and store the old Item in Items collection Response.Write "<INPUT Type=Text Name=Items Value=""" & _ Trim(Request.Form("Item")) & """><BR>"

Response.Write "<P>Please enter an Item,<BR>" Response.Write "and submit them one at a time<BR>" Response.Write "by pressing the Add Item button.<BR>" Response.Write "<INPUT Type=Text Size=50 Name=Item Value="""""">" Else ' No Item was submitted, don't show an error Response.Write "<P>Please enter an Item,<BR>" Response.Write "and submit them one at a time<BR>" Response.Write "by pressing the Add Item button.<BR>" Response.Write "<INPUT Type=Text Size=40 Name=Item Value="""""">      <BR>" End If

%>

<P> <INPUT Type="Submit" Name="Action" Value="Add Item to List"> <INPUT Type="Submit" Name="Action" Value="Submit the List"> <BR>

<% End If %>

</FORM> </BODY> </HTML>

<div class="references_section">