How to get the values from a list of inputs with the same ID by WebForms?

2

I have a list of inputs that are automatically generated by script and they are inserted into a form getting like this:

<input type="text" id="txtValue" name="txtValue" />
<input type="text" id="txtValue" name="txtValue" />
<input type="text" id="txtValue" name="txtValue" />
...

These are not elements manipulated by C # code-behind (Asp.NET WenForms application), and I need to get those values back-end when posting the form.

How do you capture these values?
Is it possible, for example, to work with these values in the form of a vector?

Obs : When parsing Request.Form I checked that only one txtValue element appeared.

    
asked by anonymous 07.10.2014 / 17:03

1 answer

1

Getting the same values on inputs with the same Id is very simple. In this type of case ASP.NET WebForms will make the content of each element available in a single string where the values will be separated by commas.

Example: 101, 104, 300 ...

To get the values then I did the following:

var values = Request.Form["txtValue"].Split(',');

The values follow the order of creation according to the nodes, and the inputs that do not receive values will come in the list as white spaces, thus making it easier to know which ones were received and which did not receive values.

Example: 101, 300, 400 ...

Notice that in this second example, after the value 101 we have two commas one after the other. This would generate a vector like:

values[0] --> "101"
values[1] --> ""
values[2] --> "300"
values[3] --> "400"
    
07.10.2014 / 17:41