How to Set Cookies?

  1. #1

    How to Set Cookies?

    Hi,

    I wan't to set a cookie with this code in codebehind

    Cookies.Set("UID", "testcookie", DateTime.Now.AddDays(30), "", "EPG", false);
    And then use

    var testcookie = Cookies.GetCookie("UID");
    to get the cookie.But in the testcookie i get:

    Expires = {0001-1-1 0:00:00}
    Value = ""
    Why lost the value "testcookie"?
    Thank you
    Last edited by geoffrey.mcgill; Dec 01, 2010 at 4:26 PM.
  2. #2
    Hi,

    Could you provide a sample to reproduce?

    It appears to be working fine.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Cookies.Set("UID", "testcookie", DateTime.Now.AddDays(30), "", "", false);
            }
        }
    
        protected void GetCookie(object sender, DirectEventArgs e)
        {
            X.Msg.Alert("Test", Cookies.GetCookie("UID").Value).Show();
        }
    </script>
    
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:Button runat="server" Text="Get cookie (server side)" OnDirectClick="GetCookie" />
        
        <ext:Button runat="server" Text="Get cookie (client side)">
            <Listeners>
                <Click Handler="alert(Ext.util.Cookies.get('UID'));" />
            </Listeners>
        </ext:Button>
        </form>
    </body>
    </html>
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi,

    Could you provide a sample to reproduce?

    It appears to be working fine.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Cookies.Set("UID", "testcookie", DateTime.Now.AddDays(30), "", "", false);
            }
        }
    
        protected void GetCookie(object sender, DirectEventArgs e)
        {
            X.Msg.Alert("Test", Cookies.GetCookie("UID").Value).Show();
        }
    </script>
    
    <!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>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:Button runat="server" Text="Get cookie (server side)" OnDirectClick="GetCookie" />
        
        <ext:Button runat="server" Text="Get cookie (client side)">
            <Listeners>
                <Click Handler="alert(Ext.util.Cookies.get('UID'));" />
            </Listeners>
        </ext:Button>
        </form>
    </body>
    </html>
    Hi,
    I've tried you code,but the result i get in client side is NULL,and in server side there will be throw an exception that the reference is null.The platform i use is:Visual 2010 & Windows XP SP3 & Internet Explorer 8.
  4. #4
    Hi,


    Confirmed, this is not work under IE. I'm not sure why.

    But it works fine under FF and Chrome.
  5. #5
    Is there a solution yet for getting this to work under IE?
  6. #6
    Hi,

    This works under IE:
    Cookies.Set("UID", "testcookie", DateTime.Now.AddDays(30), "/", null, false);
    Corrected the default values for path and domain: must be "/" and null.

Similar Threads

  1. [CLOSED] Saving grid panel settings in cookies.
    By Jean-Pierre Poulin in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 24, 2010, 5:03 PM

Posting Permissions