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。