Request与Response对象的本质:
Request对象其实是HttpRequest对象,在页面类中系统自动创建。
Request对象封装了用户请求的信息(用户输入的信息或URL参数)。
Response对象封装服务器的响应信息。
Request对象如何获取传递过来的数据?
QueryString属性(查询字符串):获取通过URL路径传来的数据。
Form属性(表单数据):获取通过表单提交传输的数据。
ServerVariables属性:获取Web服务器变量的集合。
Params属性:以上三种方式传输的内容都可以使用该属性获取。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="demo1.aspx.cs" Inherits="练习_demo1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="demo2.aspx?name=xiaowang&age=20">跳转到第二个页面</a>
</div>
</form>
</body>
</html>
不同页面之间参数传递:第一个参数之前使用?,参数之间使用&连接
protected void Page_Load(object sender, EventArgs e)
{
//string name = Request.QueryString["name"];
//string age = Request.QueryString["age"];
string name = Request.Params["name"];
string age = Request.Params["age"];
Response.Write("姓名=" + name + "年龄=" + age);
}
Response对象用于操作页面的输出流:
Write方法:直接在页面上输出内容(在页面的最顶端输出)
Redirect方法:重定向到另一个页面(通过后台控制页面跳转)