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

 

        前臺<body>中的表單代碼:html

 

ASP.NET對象有以下幾個:web

本文從「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="width: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方法,即表單的提交方法。asp.net

        表單中的action方法,指定表單的提交目標。post

        action=「WebFrom1」,指的是表單的提交後指向WebForm1窗體。在該路徑的頁面中,用Request.From能夠接受到Post方法的數據。用Requet.QuestString能夠接受Get的數據。具體用Post仍是用Get,能夠在表單中的Method屬性中設置。url

 

        後臺的C#代碼:spa

[html]  view plaincopyprint?在CODE上查看代碼片派生到個人代碼片
 
  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對象用法總結.net

1、Request對象code

        Request三種獲取表單值得方法的具體實現,我已都寫入到後代代碼的實例中了,在這裏就不贅述。

 

        這裏須要注意的是:get和post方法的區別以下:

         get方法提交,直接定義一個url就能夠傳值。缺點是,傳的值是明碼顯示的。由於瀏覽器顯示的字符是有長度的,因此他的數據顯示的時候是受限制的。

         post提交,是把數據做爲一個整個集合進行提交,對於post方法傳值的方法傳的參數不會在url中用明碼顯示。

 

2、Response對象

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

         response.write(string)的做用是從服務器端向客戶端返回數據(寫數據)。

         response.rediec("url")的做用是在服務器端重定向另外一個網頁。

相關文章
相關標籤/搜索