1,Request对象用于检索从浏览器向服务器所发送的请求的信息,他提供对当前页面请求的访问,包括标题、Cookie和查询字符串等。其常见用法包括Request.QueryString、Request。Form和Request,第3种写法是前两种的缩写,可取代前两种。Request对象的属性和方法。
属性/方法 | 说明 |
---|---|
QueryString | 取得GET请求中的数据 |
Form | 取得Post请求中的数据 |
Request | 取得POST或GET请求中的数据 |
方法(1)
使用QueryString属性获取页面间传值。在两个页面,URL参数传值是常见的GET传值方法。
Default.aspx前端代码:
<div>
<a href="ShowQueryString.aspx?Param=1">超链接传递参数</a>
</div>
ShowQueryString.aspx后端代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Request.QuestString["Param"] != null && Request.QuestString["Param"] != string.Empty)
{
string value = Request.QuestString["Param"];
Response.Write("接收的参数为" + value);
}
}
}
页面效果:
方法(2)
使用Form属性传值。通过Form表单提交的数据(通常是Post传值),可以用Request.Form属性来获取。使用Form属性获取文本框的值。
前端代码:
<body>
<% string strname = Request.Form["Name"]; %>姓名:<% =strname %>
<% string strpwd = Request.Form["Pwd"]; %>姓名:<% =strpwd %>
<div>
<p>姓名:<input id="Text1" type="text" size="20" name="Name" /></p>
<p>密码:<input id="Password1" type="password" name="Pwd" /></p>
<input id="Submit1" type="submit" value="提交" />
</div>
</body>
代码效果:
方法(3)
直接使用Request
登录传值页面后台代码:
string strname=Request["Name"];
string strpwd=Request["Pwd"];
Response.Redirect("~/WebForm1.aspx?Name="+strname);
页面接收值代码:
string name=Request["Name"];
this.Label1.Text = name;