Request.Form 和 Request.QueryString

来源:互联网 发布:算法复杂性理论 编辑:程序博客网 时间:2024/06/12 01:10

在HTML中,form表单可能包含同一名称的多个输入字段

<form id="form1" name="form1" method="get" action="">    <input type="text" name="userID" id="userID" />          <!--同名字段 text 这种情况不多见-->    <input type="text" name="fieldName" id="fieldName_0" />    <input type="text" name="fieldName" id="fieldName_1" />         <!--同名字段 checkbox-->    <!--注意:ASP.NET中的CheckBoxList在转换为HTML时为每一个chkeckbox生成单独name,如cblHobby$0、cblHobby$1-->    <input type="checkbox" name="chkHobby" id="chkHobby_0" value="0" />Game    <input type="checkbox" name="chkHobby" id="chkHobby_1" value="1" />Sprot    <input type="checkbox" name="chkHobby" id="chkHobby_2" value="2" />Music            <!--同名字段 select 多选-->    <select name="selSports" multiple>    <option value="0">Run</option>    <option value="1">Bike</option>    <option value="2">Swim</option>    </select>    <input type="submit" name="submit" id="submit" value="submit" /></form>

 

form 的method为post时,通过HttpWatch等Http分析工具可以看到,提交的Post数据如下:

userID=1&userName=Nicolas&userName=Cage&chkHobby=0&chkHobby=2&selSports=1&selSports=2&submit=submit

form的method为get时,提交的url如下:

test.html?userID=1&fieldName=Nicolas&fieldName=Cage&chkHobby=0&chkHobby=2&selSports=1&selSports=2&submit=submit


在ASP中通过

Request["fieldName"]

Request.Form["fieldName"]

Request.QueryString["fieldName"]

来访问指定字段值时,如果这个字段名称在form表单中存在多个同名的输入字段,则将返回用逗号,隔开的同名字段的所有值组成的字符串(注意,表单提交的数据形式是fieldName=value1&fieldName=value2,而不是fieldName=value1,value2)如:Nicolas,Cage,如果要取某个值,就需要手工进行拆分

ASP.NET支持ASP的三种访问方式,并对同名字段的情况进行了封装,Reqeust.Form和Request.QueryString是一个NameValueCollection集合类,集合中的每个元素都是一个键/值对,但允许存在同名键,即一个键名可以对应多个字符串值。通过GetValues 方法可以获取某个键下所有值的一个字符串数组,通过数组元素进行每个值的访问

 

HttpRequest.Form 属性
http://msdn.microsoft.com/zh-cn/library/system.web.httprequest.form.aspx

HttpRequest.QueryString 属性
http://msdn.microsoft.com/zh-cn/library/system.web.httprequest.querystring.aspx

NameValueCollection 类
http://msdn.microsoft.com/zh-cn/library/system.collections.specialized.namevaluecollection.aspx

INFO: Handling Arrays of HTML Input Elements with Request.Form and Request.QueryString
http://support.microsoft.com/viewkb/viewkb.aspx?contentid=312558

C# NameValueCollection集合
http://blog.csdn.net/byondocean/article/details/5795852
 

0 0