1

Sorry for the ambiguous question but here I go.

On each page a have a partial view displaying different theme options. These themes are just different css classes for element colors within the page. How can I allow any visitor to select a different theme, and have it persist on all following requests and sessions, even if the user is not logged in.

I imagine this must be done client side, and all I could think of is something like cookies. Unfortunately I haven't really experimented with them in ASP.NET yet and cant think of proper wording for a Google search since its such a basic concept.

If anyone can point me in the right direction i'd appreciate it. Thanks

Amry
  • 4,791
  • 2
  • 23
  • 24
Graham.Fraser
  • 369
  • 2
  • 12

1 Answers1

0

You could use a concept known as Profile

With profiles you can declare the properties you would like to expose to your users and this works for anonymous users

Basically the profile properties are stored in cookies, therefore you can configure when they should expire and other cookies-related settings

Your profile properties are compiled as part of the top-level items compilation - AKA Compilation Life-Cycle in ASP.Net, therefore they will be exposed as strongly-typed properties through the Profile class

For example:

Web.config settings

<configuration>
  <system.web>
    <anonymousIdentification enabled="true"/>
    <profile defaultProvider="AspNetSqlProfileProvider" enabled="true">
      <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add allowAnonymous="true" name="LastVisit" type="System.Nullable`1[System.DateTime]"/>
        <group name="Address">
          <add name="Street"/>
          <add name="PC"/>
          <add name="InternalNumber" type="System.Int32" defaultValue="0"/>
        </group>
        <add name="EmployeeInfo" serializeAs="Binary" type="EmployeeInfo"/>
      </properties>
    </profile>
  </system.web>
</configuration>

Consuming the profile in code (Global.asax for this example)

void Application_EndRequest(object sender, EventArgs e)
{
    if (Profile != null)
    {
        Profile.LastVisit = DateTime.Now;
        Profile.Save();
    }
}

Additionally, ASP.Net lets you access the properties in JavaScript, using Microsoft AJAX components:

Web.config

<configuration>
  <system.web.extensions>
    <scripting>
      <webServices>
        <profileService enabled="true" readAccessProperties="LastVisit" writeAccessProperties="LastVisit"/>
        <jsonSerialization maxJsonLength="102400" recursionLimit="100" />
      </webServices>
    </scripting>
  </system.web.extensions>
</configuration>

ASPX

<script type="text/javascript" src="Scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#profile").click(function () {
            Sys.Services.ProfileService.load();
            Sys.Services.ProfileService.properties.LastVisit = new Date();
            Sys.Services.ProfileService.save(
                null,
                function (m) {
                    alert(m);
                },
                function (e) {
                    alert(e);
                },
                null
            );
        });

        Sys.Services.ProfileService.load(null, function (r) {
            $("#res").append("<br/>");
            $("#res").append(Sys.Services.ProfileService.properties.LastVisit.toString());
            $("#res").append("<br/>");
        }, function (m) {
            $("#res").append(m.get_message());
        }, null);

    });
</script>

<asp:ScriptManager runat="server" ID="sm">
    <AuthenticationService />
    <ProfileService />
    <RoleService />
</asp:ScriptManager>
Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • Prefect thank you. This is why I come to stackoverflow. Through answers and people that can always point me in the right direction. – Graham.Fraser Jul 29 '12 at 03:39