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.

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.

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
Propriedades

ID do Artigo: 312558 - Última Revisão: 24 de mar de 2009 - Revisão: 1

Comentários