Feb 02 2007

ASPCode.net.Cookie released

Posted by admin under ASP.NET AJAX formally Atlas

This library (or plugin or whatever it should be called) extends Microsoft Ajax by offering easy client side support for cookies. Not much to say about it - the code to use it in your project looks like this:



        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" >
        <Scripts>
                    <asp:ScriptReference Path="scripts/aspcodecookie.js"/>
                </Scripts>

        </asp:ScriptManager>





You can then manage cookies totally serverside by calls like:



//Read cookie value
var sCookieValue = ASPCode.net.CookieManager.getCookie('acookiename');

//Set (session) cookie value
ASPCode.net.CookieManager.setCookie('acookiename', 'value' );

//Set (permanent) cookie value
ASPCode.net.CookieManager.setCookie('foo', '123bar', { expires:5 });
// above cookie expires in five days

//Set (permanent) cookie value
ASPCode.net.CookieManager.setCookie('foo', '123bar', { expires:Date.parseLocale('2010-01-01', 'yyyy-MM-dd') });
// above cookie expires in absolute date

//There are also options for setting 
//domain and path part of the cookie of course

//Delete a cookie
ASPCode.net.CookieManager.deleteCookie('acookiename');

This Cookie manager demo (inside CookieSolution.zip download) shows you how it works. It contains the exact demo you can see here - Demo of ASPCode.net.CookieManager

The other download is just the javascript file.

The code is free to use, i.e dual licensed under GPL and MIT. See the scriptfile header.

I have borrowed a lot of code and ideas from Klaus Hartl and his JQuery cookie plugin.

Links