Microsoft KB Archive/312558

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

Article ID: 312558

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



This article was previously published under Q312558



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(&quot;fieldName&quot;); 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(&quot;fieldName&quot;) Response.Write (tempArray(0)) 'Print the first value.



MORE INFORMATION
In a most generic case, you can repeat any HTML field name many times. For example:          ...     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[&quot;variablename&quot;] or Request.QueryString[&quot;variablename&quot;] 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.

