Click here to Skip to main content
15,895,084 members
Articles / Web Development / ASP.NET

Session, Cookie, Query String & Cache Variables Unified

Rate me:
Please Sign up or sign in to vote.
4.06/5 (8 votes)
1 Dec 2009Ms-PL5 min read 55.9K   297   34  
A wrapper to work with the session, cookie, query string or cache in a unified, flexible and type safe manner while offering full support for JSON serialization.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example1.aspx.cs" Inherits="Default2" %>

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 290px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <table class="style1" border="1px">
            <tr>
                <td class="style2">
                    <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="btnCounters" runat="server" Text="Hide counters" OnClick="btnShowHideCounters_Click" />
                </td>
                <td>
                    ShowCounters.Value = !ShowCounters.Value;
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="btnAddQueryKey" runat="server" Text="Add Test Key to Query String"
                        OnClick="btnAddQueryKey_Click" />
                </td>
                <td>
                    Local.SetQueryString(Keys.MyTestKey, &quot;{Test Value};&quot;, false);
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="btnRemoveMyTestKey" runat="server" Text="Remove Test Key from Query String"
                        OnClick="btnRemoveMyTestKey_Click" />
                </td>
                <td>
                    Local.SetQueryString(Keys.MyTestKey, null);
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="btnSetRandomValue" runat="server" Text="Set Value to a Random Number"
                        OnClick="btnSetRandomValue_Click" />
                </td>
                <td>
                    MyTestString.Value = new Random().Next(10000).ToString();
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="btnCopySourceToAllTargets" runat="server" Text="Call CopySourceToAllTargets"
                        OnClick="btnCopySourceToAllTargets_Click" />
                </td>
                <td>
                    MyTestString.CopySourceToAllTargets();
                </td>
            </tr>
        </table>
        <br />
        <a href="Example2.aspx">Example 2</a> <a href="Example3.aspx">Example 3</a>
    </div>
    </form>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
Mauritius Mauritius
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions