ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# 和 JavaScript Cookie 共享

C# 和 JavaScript Cookie 共享

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-12-07 15:39:00 0 删除 编辑
cookie.aspx

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




    myCookieTest
   
        .divRoot
        {
            border: dotted 1px red;
            padding: 20px;
            margin: 20px;
        }
        .divRoot h1
        {
            text-align: center;
            color: Blue;
            background-color:Gray;
        }
        hr
        {
            color: Green;
        }
   

   
        function addCookie(key,value) {
            var expireDate = new Date(); //cookie 有效日期
            expireDate.setMonth(expireDate.getMonth() + 6);
            document.cookie = key+"=" + value + "; expires=" + expireDate.toGMTString();  //添加 cookie
            alert("添加成功~!!");
            return false;
        }

        function getCookie() {
            if (document.cookie != "") {
                document.getElementById("spCookie").innerHTML = document.cookie;
            }
        }
   



   
   


       

           


                JavaScript. -> C#


           


                JavaScript. Add Cookie:


           


                Enter The Cookie Value:
               


       

       

       

           


                C# Get Cookie:


           


                The Cookie Value:
               
               


       

   

   

       

           


                C# -> JavaScript


           


                C# Add Cookie:


           


                Enter The Cookie Value:
               
               


       

       

       

           


                JavaScript. Get Cookie:


           


                The Cookie Value:
               


       

   

   




cookie.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 cookie : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnGetCookie_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies.Get("userName");

        if (cookie != null)
        {
            lblCookie.Text = cookie.Value;
        }
        else
        {
            lblCookie.Text = "NULL";
        }
    }
    protected void btnAddCookie_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("SharpCookie");
        cookie.Value = txtCookie.Text;
        Response.Cookies.Add(cookie);
        txtCookie.Text = string.Empty;
    }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-621841/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766206