如题

解决方案 »

  1.   

    没用过,但是感觉应该js和.net利用session可以直接交互吧。
    关注,学习
      

  2.   

    http://blog.csdn.net/ligaofeng/archive/2007/05/07/1599646.aspx
      

  3.   

    之前写了个示例,给你看看,希望对你有所帮助,给分吧,呵呵<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookie.aspx.cs" Inherits="easy_cookie" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>    <script language="javascript">
        
        //根据名称和参数读取cookie值 
        
    //    
    //    function GetCookie(name,params)
    //    {
    //      var originstr=document.cookie;
    //      var tmp;
    //      var reg=new RegExp("(?<=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?)","gi");   
    //      tmp=reg.exec(originstr);
    //      return tem;
    //    }    function GetCookie(name)
        {
            m=document.cookie;
        re1=new RegExp("(?!\w)"+name+"=[^;]+","");
        re2=new RegExp("^"+name+"=","");
        try
        {
          
            var a=m.match(re1)[0];
        }
        catch(e)
        {
            return null
        }
        eval("var o="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":\'")+"'}");
        return o;
        }    function ReadCookie()
        {
          var var1=document.form1.txtName.value;
          document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP;
        }
        
        function WriteCookie()
        {
           document.cookie=document.form1.txtName.value+"=IP="+document.form1.txtIP.value;
        }     
        function ClearCookie()
        {
           document.cookie="";
        }
        </script></head>
    <body>
        <form id="form1" runat="server">
            <table border="0" cellpadding="0" cellspacing="0" width="778px">
                <tr>
                    <td style="height: 100px; width: 778px;" align="center" valign="middle">
                        Cookie示例(对IP的操作)</td>
                </tr>
                <tr>
                    <td align="center" style="width: 778px">
                        <br />
                        &nbsp; &nbsp;
                        <asp:Label ID="Label1" runat="server" Text="名称"></asp:Label>
                        <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
                        &nbsp; &nbsp; &nbsp; &nbsp;
                        <asp:Label ID="Label2" runat="server" Text="IP"></asp:Label>
                        <asp:TextBox ID="txtIP" runat="server"></asp:TextBox><br />
                        <asp:Label ID="Label3" runat="server" Text="读取IP"></asp:Label>
                        <asp:TextBox ID="txtReadIP" runat="server"></asp:TextBox><br />
                    </td>
                </tr>
                <tr>
                    <td align="center" style="height: 25px; width: 778px;">
                        <br />
                        <br />
                        <asp:Button ID="btnRun" runat="server" Text="服务器设置Cookie" OnClick="Button1_Click" />
                        <asp:Button ID="btnGet" runat="server" Text="服务器读取Cookie" OnClick="Button2_Click"
                            Width="158px" />&nbsp; &nbsp;<asp:Button ID="Button3" runat="server" Text="服务器清除Cookie"
                                OnClick="Button3_Click" />
                        <br />
                        <button type="button" onclick="WriteCookie()">
                            客户端设置Cookie</button>&nbsp;
                        <button type="button" onclick="ReadCookie()">
                            客户端读取Cookie</button>&nbsp;
                        <button type="button" onclick="ClearCookie()">
                            客户端清除Cookie</button><br />
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>cs代码:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class easy_cookie : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        //写入
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Request.Cookies[txtName.Text] == null)
            {
                HttpCookie cookie = new HttpCookie(txtName.Text);
                cookie["IP"] = txtIP.Text;
                Response.Cookies.Add(cookie);
            }
            else
            {
                Response.Cookies[txtName.Text]["IP"] = txtIP.Text;
               // Response.Write("<script>alert('已存在');</script>");
            }
            
            
        }
        //读取
        protected void Button2_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies[txtName.Text];
            if (cookie!=null)
            {
                txtReadIP.Text = cookie["IP"];
            }
        }
        //清除cookie
        protected void Button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Request.Cookies.Count; i++)
            {
                HttpCookie cookie = Request.Cookies[i];
                cookie.Expires = DateTime.Now.AddDays(-1);
            }        //Response.Write(Request.Cookies.Count);
            // HttpCookie cookie = Request.Cookies;
            //cookie.Expires = DateTime.Now.Add(-1);
        }
    }
      

  4.   

    重新发一下.<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "cookie.aspx.cs "   Inherits= "easy_cookie "   %> <!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
    <html   xmlns= "http://www.w3.org/1999/xhtml "> 
    <head   runat= "server "> 
            <title> 无标题页 </title>         <script   language= "javascript "> 
            
            //根据名称和参数读取cookie值   
            
    //         
    //         function   GetCookie(name,params) 
    //         { 
    //             var   originstr=document.cookie; 
    //             var   tmp; 
    //             var   reg=new   RegExp( "(? <=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?) ", "gi ");       
    //             tmp=reg.exec(originstr); 
    //             return   tem; 
    //         }         function   GetCookie(name) 
            { 
                    m=document.cookie; 
            re1=new   RegExp( "(?!\w) "+name+ "=[^;]+ ", " "); 
            re2=new   RegExp( "^ "+name+ "= ", " "); 
            try 
            { 
                
                    var   a=m.match(re1)[0]; 
            } 
            catch(e) 
            { 
                    return   null 
            } 
            eval( "var   o= "+a.replace(re2, "{ ").replace(/&/g, " ', ").replace(/=/g, ":\ ' ")+ " '} "); 
            return   o; 
            }         function   ReadCookie() 
            { 
                var   var1=document.form1.txtName.value; 
                document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP; 
            } 
            
            function   WriteCookie() 
            { 
                  document.cookie=document.form1.txtName.value+ "=IP= "+document.form1.txtIP.value; 
            }           
            function   ClearCookie() 
            { 
                  document.cookie= " "; 
            } 
            </script> </head> 
    <body> 
            <form   id= "form1 "   runat= "server "> 
                    <table   border= "0 "   cellpadding= "0 "   cellspacing= "0 "   width= "778px "> 
                            <tr> 
                                    <td   style= "height:   100px;   width:   778px; "   align= "center "   valign= "middle "> 
                                            Cookie示例(对IP的操作) </td> 
                            </tr> 
                            <tr> 
                                    <td   align= "center "   style= "width:   778px "> 
                                            <br   /> 
                                            &nbsp;   &nbsp; 
                                            <asp:Label   ID= "Label1 "   runat= "server "   Text= "名称 "> </asp:Label> 
                                            <asp:TextBox   ID= "txtName "   runat= "server "> </asp:TextBox> <br   /> 
                                            &nbsp;   &nbsp;   &nbsp;   &nbsp; 
                                            <asp:Label   ID= "Label2 "   runat= "server "   Text= "IP "> </asp:Label> 
                                            <asp:TextBox   ID= "txtIP "   runat= "server "> </asp:TextBox> <br   /> 
                                            <asp:Label   ID= "Label3 "   runat= "server "   Text= "读取IP "> </asp:Label> 
                                            <asp:TextBox   ID= "txtReadIP "   runat= "server "> </asp:TextBox> <br   /> 
                                    </td> 
                            </tr> 
                            <tr> 
                                    <td   align= "center "   style= "height:   25px;   width:   778px; "> 
                                            <br   /> 
                                            <br   /> 
                                            <asp:Button   ID= "btnRun "   runat= "server "   Text= "服务器设置Cookie "   OnClick= "Button1_Click "   /> 
                                            <asp:Button   ID= "btnGet "   runat= "server "   Text= "服务器读取Cookie "   OnClick= "Button2_Click " 
                                                    Width= "158px "   /> &nbsp;   &nbsp; <asp:Button   ID= "Button3 "   runat= "server "   Text= "服务器清除Cookie " 
                                                            OnClick= "Button3_Click "   /> 
                                            <br   /> 
                                            <button   type= "button "   onclick= "WriteCookie() "> 
                                                    客户端设置Cookie </button> &nbsp; 
                                            <button   type= "button "   onclick= "ReadCookie() "> 
                                                    客户端读取Cookie </button> &nbsp; 
                                            <button   type= "button "   onclick= "ClearCookie() "> 
                                                    客户端清除Cookie </button> <br   /> 
                                    </td> 
                            </tr> 
                    </table> 
            </form> 
    </body> 
    </html>
    cs代码: 
    using   System; 
    using   System.Data; 
    using   System.Configuration; 
    using   System.Collections; 
    using   System.Web; 
    using   System.Web.Security; 
    using   System.Web.UI; 
    using   System.Web.UI.WebControls; 
    using   System.Web.UI.WebControls.WebParts; 
    using   System.Web.UI.HtmlControls; public   partial   class   easy_cookie   :   System.Web.UI.Page 

            protected   void   Page_Load(object   sender,   EventArgs   e) 
            {         } 
            //写入 
            protected   void   Button1_Click(object   sender,   EventArgs   e) 
            { 
                    if   (Request.Cookies[txtName.Text]   ==   null) 
                    { 
                            HttpCookie   cookie   =   new   HttpCookie(txtName.Text); 
                            cookie[ "IP "]   =   txtIP.Text; 
                            Response.Cookies.Add(cookie); 
                    } 
                    else 
                    { 
                            Response.Cookies[txtName.Text][ "IP "]   =   txtIP.Text; 
                          //   Response.Write( " <script> alert( '已存在 '); </script> "); 
                    } 
                    
                    
            } 
            //读取 
            protected   void   Button2_Click(object   sender,   EventArgs   e) 
            { 
                    HttpCookie   cookie   =   Request.Cookies[txtName.Text]; 
                    if   (cookie!=null) 
                    { 
                            txtReadIP.Text   =   cookie[ "IP "]; 
                    } 
            } 
            //清除cookie 
            protected   void   Button3_Click(object   sender,   EventArgs   e) 
            { 
                    for   (int   i   =   0;   i   <   Request.Cookies.Count;   i++) 
                    { 
                            HttpCookie   cookie   =   Request.Cookies[i]; 
                            cookie.Expires   =   DateTime.Now.AddDays(-1); 
                    }                 //Response.Write(Request.Cookies.Count); 
                    //   HttpCookie   cookie   =   Request.Cookies; 
                    //cookie.Expires   =   DateTime.Now.Add(-1); 
            } 

      

  5.   

    bnb2626是可以的,最好再加上一些编码~~
      

  6.   

    不好意思,这几天忙着每看CSDN,今天看了,给分了
    谢谢