Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# to iterate form fields with same name

I have a section of a form that I need to handle differently from the rest of form results. In the section that needs special handling I need to iterate over 3 form fields that have the same name. They have to have the same name, I can't change it. The section of the form I am referring to looks something like this:

<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>
<td><input name="Color" size="20" value="" type="text"></td>

Using C# I try something like this:

I try to handle it like this:

int i;

for (i = 1; i <= Request.Form["Color"][i]; i++)
{
    colorName.Text += Request.Form["Color"];
}

Which leads to the following exception:

System.NullReferenceException: Object reference not set to an instance of an object.

How should I be handling form fields with the same name?

like image 609
itsatrp Avatar asked Apr 05 '10 21:04

itsatrp


1 Answers

You don't need to do any splits or other special magic; you can simply get a string array from ASP.NET:

string[] values = Request.Form.GetValues("Color");

like image 192
pdwetz Avatar answered Sep 25 '22 01:09

pdwetz