在 C# ASP.NET 中获取发布的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9956180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get posted value In C# ASP.NET
提问by amd
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
我知道如何使用:Request.Form["myField"] 在 aspnet 中获取单个字段的值,但在这里我有多个字段,我不知道这些字段的数量,因为它们是动态的
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'];
在 php 中,我通过访问以数组形式获取值$_POST['orders'];
ex:
前任:
$orders = $_POST['orders'];
foreach($orders as $order){
//execute ...
}
how can I do this in c# ?
我怎样才能在 c# 中做到这一点?
采纳答案by BrunoLM
Request.Form is a NameValueCollection, an object that can store a collection of items under the same key and the ToStringdisplays the values in CSV format.
Request.Form 是一个NameValueCollection对象,可以在同一个键下存储一组项目,并ToString以 CSV 格式显示值。
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)
{
}
}
回答by Code Maverick
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.
你会使用Request.Form[]. 或者,如果您的表单和字段有runat="server"id 和 id,您可以只使用代码隐藏中的 id 和.Text()访问其值的方法。
回答by Darren
You can access everything that gets sent back to the server by using the Request object.
您可以使用 Request 对象访问发送回服务器的所有内容。
Request.Form.Items
Is a collection that will contain the item you are looking for.
是一个包含您要查找的项目的集合。

