Request对象

Request对象是HttpRequest类的一个实例他的作用是读取客户端在Web请求的过程中传送的参数。
在访问一个网页的时候,在浏览器的地址栏中输入网址,即可显示网页。为什么浏览器需要用这个路径名和名称组成的网址?是因为WWW是一个无序的环境所以需要采用某种操作来让服务器识别每个客户端,全路径和名称的组合仅仅是在请求页面的浏览器时向Web服务器发送一个值。
Request对象_第1张图片

获取用户提交的信息

方法1:QueryString

使用Querystring来获得上一个页面传递过来的字符串参数
例如,第一个页面Page1有一个连接是指向Page2的,在连接的过程中,Page1可以传递给Page2一些需要的参数,方式如下:

在Page1中的代码
<a href="Page2.aspx?ID=6&Name=Zhang">查看a>

在Page2中的代码
Request.Querystring["ID"]
Request.Querystring["Name"]

这种传递的方式使用的是表单中的GET方式。

方法2:form方法

首先建立一个 index.aspx
页面代码完全采用HTML标签书写
代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>title>
    <style type="text/css">
        .auto-style1 {
            width: 388px;
        }
    style>
head>
<body>
    <form id="MyForm" name="MyForm" method="post" action="view.aspx">
        <table style="width:100%;">
            <tr>
                <td class="auto-style1">用户名:td>
                <td>
                    <input id="txtName" name="txtName" type="text" />td>
            tr>
            <tr>
                <td class="auto-style1">密码:td>
                <td>
                    <input id="txtPwd" name="txtPwd" type="password" />td>
            tr>
            <tr>
                <td class="auto-style1">
                    <input id="SubmitForm" name="SubmitForm" type="submit" value="提交" />td>
                <td>
                    <input id="ResetForm" name="ResetForm" type="reset" value="重置" />td>
            tr>
        table>
    form>
body>
html>

view.aspx.cs中代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class view : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request.Form["txtName"];//获得表单中的txtName文本框的值
        string password = Request.Form["txtpwd"];//获得表单中txtpwd中的值
        Response.Write("

您的信息如下:

"
); Response.Write("

用户名:"+name); Response.Write("

密码为:" + password); } }

方法3:通过Request对象传递参数

这时候首页index.aspx 不能使用HTML编写,而是使用ASP.NET控件,运行在服务器端。
可以通过表单使用GET形式传递参数,并在后台程序中使用QueryString方法获得输入值。
工具采用的是工具箱中的标准 ASP.NET控件
index2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index2.aspx.cs" Inherits="index2" %>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>title>
    <style type="text/css">
        .auto-style1 {
            width: 330px;
        }
    style>
head>
<body>
    <form id="form1" runat="server" action="view2.aspx">    
        <table style="width:100%;">
            <tr>
                <td class="auto-style1">用户名:td>
                <td>
                    <asp:TextBox ID="txtName" name="txtName" runat="server">asp:TextBox>
                td>
                <td> td>
            tr>
            <tr>
                <td class="auto-style1">密码:td>
                <td>
                    <asp:TextBox ID="txtPwd" name="txtPwd" runat="server" TextMode="Password">asp:TextBox>
                td>
                <td> td>
            tr>
            <tr>
                <td class="auto-style1">
                    <asp:Button ID="btnLogin" runat="server" Text="提交" OnClick="btnLogin_Click" />
                td>
                <td>
                    <asp:Button ID="btnReset" runat="server" Text="重置" />
                td>
                <td> td>
            tr>
        table>
    <div>

    div>
    form>
body>
html>

index2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogin_Click(object sender, EventArgs e)//双击那个按钮自动跳转和生成
    {
        Response.Redirect("view2.aspx?name1=" + txtName.Text + "&pwd1=" + txtPwd.Text);//?参数名=参数值&参数名=参数值……

    }

}

view2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class view2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string namew = Request.QueryString["name1"];
        string passwordw = Request.QueryString["pwd1"];
        Response.Write("

您的信息如下:

"
); Response.Write("

用户名:" + namew); Response.Write("

密码为:" + passwordw); } }

方法4:cookie方法

    Cookie 是一小段文本信息,伴随着用户请求的页面在Web服务器和浏览    器之间传递。
    Cookie和Session、Application类似,用来保存相关信息。但是其不同是Cookie信息保存在客户端,而另外两种保存在服务器端。

    ASP.NET包含两个内部Cookie内部集合
    1.HttpRequest的Cookie集合,是从客户端传送到服务器的Cookie
    2.HttpResponse的Cookie集合,包含的是一些新的Cookie,这些Cookie在服务器上创建,然后传输到客户端。

Cookie属性:

  • Name:获取或设置Cookie名称
  • Value:获取或设置Cookie值
  • Expires:获取或者设置Cookie的过期日期和时间
  • Version:获取或设置此Cookie符合的HTTP状态维护版本

Cookie对象的方法:

  • Add 新增一个Cookie变量
  • Clear
  • Get
  • GetKey
  • Remove

Cookie 分为两类

  1. 会话Cookie
    仅在浏览器中保留的Cookie,为暂时性的,关闭浏览器就消失。
HttpCookie cook=new HttpCookie("username","张三");
Response.Cookie.Add(cook);
  1. 持久性Cookie
    以文件的形式保存在客户端的。
HttpCookie cook=new HttpCookie("username","张三");
cook.Expires = DateTime.Now.AddDays(30);//当前日期加30天
Response.Cookie.Add(cook);

方法4:Session方法(最常用的一种方法)

Session是指一个客户端用户与服务器进行通信的时间间隔,通常指从登陆进入系统到注销退出系统之间所经历的时间。具体到Web中的Session是用户浏览这个网站所花费的时间。

可以通过TimeOut属性设置Session有效期限,默认20min

Session对象是不需要实例的

方法1:Session.Add("变量名""变量值");
方法2:Session["变量名"]=变量值;

你可能感兴趣的:(asp-net)