读取cookie

《ASP.NET 3.5 揭秘》

使用Request.Cookies集合可以读取cookie值

View Code
 1 <%@ Page Language="C#" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <script runat="server">
 6     void Page_Load()
 7     {
 8         if (Request.Cookies["message"] != null)
 9         {
10             lblCookieValue.Text = Request.Cookies["message"].Value; 
11         } 
12     }
13 </script>
14 
15 <html xmlns="http://www.w3.org/1999/xhtml">
16 <head runat="server">
17     <title>Get Cookie</title>
18 </head>
19 <body>
20     <form id="form1" runat="server">
21     <div>
22     
23     The value of the message cookie is:
24     <asp:Label
25         id="lblCookieValue"
26         Runat = "server" />
27     </div>
28     </form>
29 </body>
30 </html>


如果想读出Request.Cookies集合中所有的cookie值,可以这样。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void Page_Load()
    {
        ArrayList colCookies = new ArrayList();
        for (int i = 0; i < Request.Cookies.Count; i++)
        {
            colCookies.Add(Request.Cookies[i]); 
        }

        grdCookies.DataSource = colCookies;
        grdCookies.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Get All Cookies</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:GridView
        id="grdCookies"
        Runat="server" />
    </div>
    </form>
</body>
</html>

 

转载于:https://www.cnblogs.com/hellolong/archive/2012/11/15/2771450.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值