Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

INFO: Handling Arrays of HTML Input Elements with Request.Form and Request.QueryString


Summary

A Hypertext Markup Lanugage (HTML) form may include several input fields that share the same name. If you use the Request.Form or Request.QueryString collection to access a field name, a string is returned that contains all values from all of the fields with the given field name, which are separated by a comma (,).

To access individual values, you can use the GetValues method to obtain an array of strings. You can then use the array elements to provide access to individual values. For example, in Microsoft Visual C# .NET, use the following code:
String[] tempArray;
tempArray = Request.Form.GetValues("fieldName");
Response.Write (tempArray[0]); //Print the first value.
In Microsoft Visual Basic .NET, use the following code:
Dim tempArray() as String
tempArray = Request.Form.GetValues("fieldName")
Response.Write (tempArray(0)) 'Print the first value.

↑ Back to the top


More Information

In a most generic case, you can repeat any HTML field name many times. For example:
  <form ... >
<input type="text" name="fieldName" ... >
<input type="text" name="fieldName" ... >
...
<input type="submit" ... >
</form>
To support this case, the Request.Form and Request.QueryString collections should have an array of string values for each field name. Request.Form and Request.QueryString are collections of strings.

Because this is not a common scenario for most users, the default behavior of Request.Form["variablename"] or Request.QueryString["variablename"] returns a single string. Thus, users who need to use multivalue fields must use the Request.Form.GetValues method.

Comparison with Classic ASP

To resolve this dilemma, classic Microsoft Active Server Pages (ASP) and Microsoft Visual Basic Scripting Edition (VBScript) rely on default properties in OLE Automation. Request.Form and Request.QueryString lookup always return an OLEAUT collection of strings. In addition, these collections have a default property of type string, which is automatically dereferenced on assignments. This works well for VBScript; however, in classic ASP, the interfaces are very difficult to use in Microsoft Visual C++.

You cannot use this approach in ASP.NET or Visual Basic .NET because there is no difference between LET and SET, and there is no automatic default property resolution.

↑ Back to the top


References

For additional information about ASP.NET features, click the article number below to view the article in the Microsoft Knowledge Base:

305140 INFO: ASP.NET Roadmap

↑ Back to the top


Keywords: kbvs2003swept, kbdsupport, kbhtml, kbinfo, kb

↑ Back to the top

Article Info
Article ID : 312558
Revision : 4
Created on : 6/10/2019
Published on : 6/10/2019
Exists online : False
Views : 150