取多个name值相同的input里面的值

html 中是允许多个具有相同name属性的元素的,例如
服务器端读取的常规做法是:

string name = Request.Params["txtName"];

得到的将是一串以逗号分割的字符串,当然你可以手动分割:
string[] nameParts = name.Split(',');
但是当每个 input 输入可能包含逗号的时候,通过逗号分割就会是错的。
如何解决?
在 Classic ASP 通过 Request 可以这样分别获取

<%
firstName = Request.Form("txtName")(1)
middleName = Request.Form("txtName")(2)
lastName = Request.Form("txtName")(3)
%>
在 ASP.NET HttpRequest 同样支持 Classic ASP Request 的用法,

string[] nameParts = Request.Params.GetValues("txtName");
string firstName = nameParts[0];
string middleName = nameParts[1];
string lastName = nameParts[2];
以上用法对于 GET/POST 方式提交都是适用的。

值 得注意的是,用来存储 QueryString/Form/ServerVariables 的对象是 System.Collections.Specialized.NameValueCollection, 这是 Key/Value 型对象,它的特殊性在于,一个Key下可存储多个 Value。

posted @ 2013-08-04 02:25  nd  阅读(6714)  评论(1编辑  收藏  举报