Get published value in C # ASP.NET

I have a dynamic form that allows me to dynamically add many fields,

I know how to get the value of a single field in aspnet using: Request.Form ["myField"], but here I have more fields and I don’t know the count of these fields, since they are dynamic

the name of the fields is "orders []"

Example:

<form> <input type="text" name="orders[]" value="order1" /> <input type="text" name="orders[]" value="order2" /> <input type="text" name="orders[]" value="order3" /> </form> 

In php, I get the values ​​as an array, referring to $_POST['orders'] ;

Example:

 $orders = $_POST['orders']; foreach($orders as $order){ //execute ... } 

how to do it in c #?

+8
c # post
source share
3 answers

Use Request.Form.GetValues.

Request.Form is a NameValueCollection , an object that can store a collection of elements under the same key, and ToString displays values ​​in CSV format.

Markup:

 <input type="text" name="postField[]" /> <input type="text" name="postField[]" /> <input type="text" name="postField[]" /> <input type="text" name="postField[]" /> <asp:Button Text="text" runat="server" OnClick="ClickEv" /> 

Code behind:

 protected void ClickEv(object sender, EventArgs e) { var postedValues = Request.Form.GetValues("postField[]"); foreach (var value in postedValues) { } } 
+10
source share

You would use Request.Form[] . Or, if your form and fields have runat="server" and identifiers, you can simply use the identifier in the code and the .Text() method to access its value.

+3
source share

You can access everything that is sent back to the server using the Request object.

 Request.Form.Items 

This is the collection that will contain the item you are looking for.

+1
source share

All Articles