Response和Request

login.htm

<form method="post" action="login.aspx">
  //  method有两种方法是post和get,post对应于Request.Form,  get对应于Request.Querty.string
  // 用post 时URL不会加长,但用get 时URL会加长,所有传递参数以&&的形式显示在URL中
  //  由于起特性,在新闻系统中将用到 get

    
<table style="width: 318px">
        
<tr>
            
<td style="width: 107px; height: 52px">
            
</td>
            
<td style="width: 291px; height: 52px">
                
<input id="textUserName" name="textUserName" type="text" /></td>
            
<td style="width: 96px; height: 52px">
            
</td>
        
</tr>
        
<tr>
            
<td style="width: 107px; height: 70px">
            
</td>
            
<td style="width: 291px; height: 70px">
                
<input id="textUserPwd" name="textUserPwd" type="text" /></td>
            
<td style="width: 96px; height: 70px">
            
</td>
        
</tr>
        
<tr>
            
<td style="width: 107px; height: 67px">
            
</td>
            
<td style="width: 291px; height: 67px; text-align: center">
                
<input id="btnSubmit" type="submit" value="提交"  /></td>
            
<td style="width: 96px; height: 67px">
            
</td>
        
</tr>
    
</table>


</form>

 login.aspx.cs

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
...{
    
protected void Page_Load(object sender, EventArgs e)
    
...{
        
//string userName = Request["textUserName"].ToString();
        
//string userPwd = Request["textUserPwd"].ToString();
        string userName = Request.Form.Get("textUserName").ToString();
        
string userPwd = Request.Form.Get("textUserPwd").ToString();
        
//以上两种方法适用于 method=post,任意一种均可



        
string userName = Request.QueryString["textUserName"].ToString();
        
string userPwd = Request.QueryString["textUserPwd"].ToString();
        
// 以上方法适用于 mrthod=get



        Response.Write(
"登录的用户名是:"+userName+";密码是"+userPwd);
        
// 用于输出结果



        
if (userName == "zuo" && userPwd == "zuo")
        
...{
            Response.Redirect(
"login2.aspx");
        }

        
else
        
...{
            Response.Redirect(
"login.htm");
        }

        
// 以上方法用于验证用户名和密码,然后重新定位
    }

}

几点注意:

1. 在aspx文件中,Form只能出现一次,且无action属性,默认为其本身,但htm必须有action属性

     <form id="form1" method="post" runat="server"></form>

2.  post---Request.Form       get---Request..QueryString

3. Response的两个属性 Write 和 Redirect

posted @ 2007-04-12 15:44  ermen  阅读(133)  评论(0)    收藏  举报