使用get方法:
<form id="form1"method="get"runat="server"><div>name<asp:TextBox ID="name"runat="server"></asp:TextBox>website<asp:TextBox ID="website"runat="server"></asp:TextBox><asp:Button ID="Button1"runat="server"Text="send"/></div></form>
接收用get 方法传输的数据的写法:
protectedvoidPage_Load(objectsender, EventArgs e){stringid=Request.QueryString["name"];stringwebsite=Request.QueryString["website"];Response.Write(id+"< br>"+website);Response.Write("你使用的是"+Request.RequestType+"方式传送数据");}
使用post方法:
<form id="form2"method="post"runat="server"><div>你的名字<asp:TextBox ID="name2"runat="server"></asp:TextBox>你的网站<asp:TextBox ID="website2"runat="server"></asp:TextBox><asp:Button ID="Button2"runat="server"Text="send"/>学习request 和 response的用法</div></form>
接收用post 方法传输的数据的写法:
protectedvoidPage_Load(objectsender, EventArgs e){stringid2=Request.Form["name2"];stringwebsite2=Request.Form["website2"];Response.Write(id2+"< br>"+website2);Response.Write("你使用的是"+Request.RequestType+"方式传送数据");}
同时接受get和post 方法传送数据的代码写法:
<form id="form3"method="post" action="Login.aspx?action=login"runat="server"><div>你的名字<asp:TextBox ID="name3"runat="server"></asp:TextBox>你的网站<asp:TextBox ID="website3"runat="server"></asp:TextBox><asp:Button ID="Button3"runat="server"Text="send"/>学习request 和 response的用法</div></form>
使用action参数 模拟get方法,如果是asp 提交时action参数 可以省略。如果是ajax, action 参数不能省略。
同时可以通过action参数 来区分同一个页面多个form 提交的情况。
//A 写法
stringaction= Request.Params("action");stringid3=Request.Params["name3"];stringwebsite3=Request.Params["website3"];Response.Write(id3+"< br>"+website3);
//B 写法
stringaction= Request.QueryString("action");stringid3= Request.Params("name3");stringwebsite3=Request.Params["website3"];Response.Write(id3+"< br>"+website3);
注意:
A写法:action :地址栏后面的参数不能和form 里面某字段的参数名称相同,
B写法:没有这个问题
eg:
<form id="form3"method="post" action="Login.aspx?action=login"runat="server"><div>你的名字<asp:TextBox ID="name3"runat="server"></asp:TextBox>你的网站<input type="text" name="Action" id="Action1" value="Login" /><asp:TextBox ID="website3"runat="server"></asp:TextBox><asp:Button ID="Button3"runat="server"Text="send"/>学习request 和 response的用法</div></form>
stringid3= Request.Params("action"); // 得到 两个值,中间用逗号连接 "login,Login"// 同时可见Request.Params不区分大小写
表单提交中,的Get和Post方式的区别归纳如下 几点:
1. get是从服务器上获取数据,post是向服务器传送数据。
2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到。post是通过HTTP post机制,将表单内各个字段与其内容放置在HTML HEADER内一起传送到ACTION属性所指的URL地址。用户看不到这个过程。
3. 对于get方式,服务器端用Request.QueryString获取变量的值,对于post方式,服务器端用Request.Form获取提交的数 据。
4. get传送的数据量较小,不能大于2KB。post传送的数据量较大,一般被默认为不受限制。但理论上,IIS4中最大量为80KB,IIS5中为 100KB。
5. get安全性非常低,post安全性较高。但是执行效率却比Post方法好。
建议:
1、get方式的安全性较Post方式要差些,包含机密信息的话, 建议用Post数据提交方式;
2、在做数据查询时,建议用Get方式;而在做数据添加、修改或删 除时,建议用Post方式。
params、Request、Request.querystring、Request.Form 具体区别!
MSDN:Request ObjectRequest
Request.Form:获取以POST方式提交的数据(接收Form提交来的数 据);
Request.QueryString:获取地址栏参数(以GET方式提交的数据)
Request:包含以上两种方式(优先获取 GET方式提交的数据),它会在QueryString、Form、ServerVariable中都按先后顺序搜寻一遍。而且有时候也会得到不同的结 果。如果你仅仅是需要Form中的一个数据,但是你使用了Request而不是Request.Form,那么程序将在QueryString、 ServerVariable中也搜寻一遍。如果正好你的QueryString或者ServerVariable里面也有同名的项,你得到的就不是你原 本想要的值了。
Request.Params是所有post和get传过来的值的集合,request.params其实是一个集合,它依次包括 request.QueryString、request.Form、request.cookies和request.ServerVariable。
默认虽然是POST Form,但是只是自己post自己,不能POST到其他页面
如果非要提交到令一个页面的 话 用HTML元素 把runat="server" 去掉,同时添加action参数,用submit提交 用Request.Form["xxx"] 可以取值
只要是POST方法,无论是通过form的submit 提交,还是ajax $post(), 都是用Request.Form["xxx"] 取值。