可以通过遍历 Request 对象的 Cookie 集合可以获得所有的浏览器所有的 Cookie。方法如下:
foreach (string strKey in Request.Cookies)
{
lblCookies.Text += strKey + "=" + Request.Cookies[ strKey ].Value;
}
(1)优点和缺点
优点:
1.使用简单,是保持用户状态的一种非常常用的方法。比如在购物网站中用户跨多个页面表单时可以
用它来保持用户状态。
缺点:
1.常常被人认为用来收集用户隐私而遭到批评。
(2)使用方法
1.在源页面的代码中创建你需要传递的名称和值构造 Cookie 对象:
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");
Response.Cookies.Add(cookie);
2.在目的页面的代码使用 Cookie 对象取出传递的值:Result = Request.Cookies[ "myCookie" ].Value;
(3)应用举例
1.源页面 *.aspx 的代码:
private void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie objCookie = new HttpCookie("myCookie","Hello,Cookie!");
Response.Cookies.Add(objCookie);
}
2.目的页面 *.aspx 的代码:
private void Page_Load(object sender, System.EventArgs e)
{
string myName1Value;
myName1Value = Request.Cookies[ "myCookie" ].Value;
}
五、使用 Server.Transfer
使用 Server.Transfer 变量是在页面间传递值的第五种方式。上面的四个方法我们在 ASP 中常常使用,
但是这个方法是在 ASP.NET 中新出现的。Server.Transfer 是从当前的 ASPX 页面转到新的 ASPX 页面,服
务器端执行新页并输出,在新页面中通过 Context.Handler 来获得前一个页面传递的各种数据类型的值、表单
数据、QueryString.由于重定向完全在服务器端完成,所以客户端浏览器中的 URL 地址是不会改变的。
调用 Server.Transfer 时,当前的 ASPX
页面终止执行,执行流程转入另一个 ASPX 页面,但新的 ASPX
页面仍使用前一 ASPX 页面创建的应答流。[2]
在这里比较一下 Server.Transfer 和在“一”中使用的 Response.Redirect 的区别。
(1)Server.Transfer 在服务器端完成,所以客户端浏览器中的 URL 地址是不会改变的;Response.Redirect
是客户端完成,向服务器端提出新的页面处理请求,所以客户端浏览器中的 URL 地址是会改变的。
(2)Server.Transfer 在服务器端完成,不需要客户端提出请求,减少了客户端对服务器端提出请求。[2]
评论0
最新资源