ASP.NET中的Request和Respone对象的使用

        前台<body>中的表单代码:

ASP.NET对象有如下几个:

本文从“asp.net中通过from表单submit提交到后台的实例”来谈谈RequestResponse这两个对象的使用。

(一)引入实例

 1 [html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 2 <span style="font-size:14px;"><body>  
 3     <form method="get" action="WebForm1.aspx">  
 4         <table style="50%;">  
 5             <tr>  
 6                 <td> </td>  
 7                 <td>  
 8                     <input id="text1"  name="txtUserName" type="text" /></td>  
 9                 <td class="auto-style1"> </td>  
10             </tr>  
11             <tr>  
12                 <td> </td>  
13                 <td>  
14                     <input id="text2"  name="txtUserPwd" type="text" /></td>  
15                 <td class="auto-style1"> </td>  
16             </tr>  
17             <tr>  
18                 <td> </td>  
19                 <td>  
20                     <input id="ccc" type="submit" value="提交" /></td>  
21                 <td class="auto-style1"> </td>  
22             </tr>  
23         </table>  
24     </form>  
25 </body></span>  

 表单中的method方法,即表单的提交方法。

        表单中的action方法,指定表单的提交目标。

        action=“WebFrom1”,指的是表单的提交后指向WebForm1窗体。在该路径的页面中,用Request.From可以接受到Post方法的数据。用Requet.QuestString可以接受Get的数据。具体用Post还是用Get,可以在表单中的Method属性中设置。

        后台的C#代码:

  1. <span style="font-size:14px;">    public partial class WebForm1 : System.Web.UI.Page  
  2.     {  
  3.         protected void Page_Load(object sender, EventArgs e)  
  4.         {  
  5.             //Request三种获取表单值得方法。  
  6.   
  7.             #region  对于post方法递交表单的获取值方法  
  8.             //string userName = Request.Form.Get("txtUserName").ToString();  
  9.             //string userPwd = Request.Form.Get("txtUserPwd").ToString();  
  10.             #endregion  
  11.   
  12.             #region  对于get方法递交表单的获取值方法  
  13.             //string userName = Request.QueryString["txtUserName"].ToString();    
  14.             //string userPwd = Request.QueryString["txtUserPwd"].ToString();  
  15.             #endregion  
  16.              
  17.             #region  对两者方法都适用的方法,运用Reuqest的索引值去获取所要求的表单值  
  18.             string userName = Request["txtUserName"].ToString();  
  19.             string userPwd = Request["txtUserPwd"].ToString();  
  20.             #endregion  
  21.             Response.Write("登陆的用户名为:" + userName + ";密码为:" + userPwd);  
  22.   
  23.             if (userName=="a"&&userPwd=="b")  
  24.             {  
  25.                 Response.Redirect("WebForm2.aspx");  
  26.             }  
  27.             else  
  28.             {  
  29.                 Response.Redirect("login.html");  
  30.             }         
  31.         }  
  32.     }</span>  

(二)Request对象和Response对象用法总结

一、Request对象

        Request三种获取表单值得方法的具体实现,我已都写入到后代代码的实例中了,在这里就不赘述。

        这里需要注意的是:get和post方法的区别如下:

         get方法提交,直接定义一个url就可以传值。缺点是,传的值是明码显示的。因为浏览器显示的字符是有长度的,所以他的数据显示的时候是受限制的。

         post提交,是把数据作为一个整个集合进行提交,对于post方法传值的方法传的参数不会在url中用明码显示。

二、Response对象

         response对象,最主要的用到的方法是respone.write(string)和responst.redirect(url).

         response.write(string)的作用是从服务器端向客户端返回数据(写数据)。

         response.rediec("url")的作用是在服务器端重定向另一个网页。

原文地址:https://www.cnblogs.com/SJP666/p/4871902.html