Get posted value In C# ASP.NET

2019-04-22 21:28发布

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

I Know how to get the value of single field in aspnet using : Request.Form["myField"],but here i have more than field and i dont know the count of these fields since these are dynamic

the fields name is "orders[]"

ex:

<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 by accessing $_POST['orders'];

ex:

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

how can I do this in c# ?

3条回答
闹够了就滚
2楼-- · 2019-04-22 21:57

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

Request.Form.Items

Is a collection that will contain the item you are looking for.

查看更多
放我归山
3楼-- · 2019-04-22 22:08

Use Request.Form.GetValues.

Request.Form is a NameValueCollection, an object that can store a collection of items under the same key and the ToString displays the 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)
    {

    }
}
查看更多
别忘想泡老子
4楼-- · 2019-04-22 22:15

You would use Request.Form[]. Or if your form and fields had runat="server" and ids, you could just use the id in the codebehind and the .Text() method to access its value.

查看更多
登录 后发表回答