Take the 2-minute tour ×

On the site I'm developing, there is a page that lists all of the users on the site (and some other information) in a table. This page is identical to everyone who logs in (at any given time, at least), except for one difference - their own row is highlighted. So it seemed to me that this would be an ideal candidate for caching - we don't need every user that hits that page to recalculate and render the data on the page when only that one thing changes.

I attempted to handle this by adding output caching to the page, and moving the highlighting of the table row to JavaScript (a jQuery call) - my hope was that this page would be cached, but the JavaScript would be run every time somebody hits it, resulting in their own row being highlighted. That doesn't seem to be the case, however. If I hit the site first, my row is highlighted. Then, if somebody else hits the page within the cache expiry time (on a different machine, so it's not a browser cache thing), they see the page with my row highlighted rather than their own, until the cache expires - and then everybody sees the row highlighted of whoever hit the page next, and so on. It seems like it is serving up the cached version of the page after the JavaScript has been executed, rather than serving up the cached version of the page and then running the JavaScript.

Is there a way to do what I'm trying to do - cache the page so I'm not executing ASP.NET calls every time somebody hits the page, but have the JavaScript run fresh every time the page is loaded so a different row gets highlighted? Or if not with JavaScript is there any way to do this? I thought about using setInterval to do the check for a highlight (and re-highlight the proper row) at regular intervals, but that seems like overkill since this only needs to be done once after the page is loaded, and never done again until the page is reloaded.

Here are the relevant parts of my aspx file:


<script type="text/javascript">

jQuery(document).ready(function($) {
    var isAuthenticated = ('<%= Context.User.Identity.IsAuthenticated %>' == 'True');

    if (isAuthenticated) {
        var owner = '<%= Context.User.Identity.Name %>';

        $('#' + owner).css("background-color", "#bdf");



<% foreach (var item in Model.Items) { %>

    <%= String.Format("<tr id=\"{0}\">", item.Owner) %>
            <%= Html.Encode(item.Owner) %>

<% } %>

share|improve this question
Check out my answer for an alternative using the Substitution control, which would save you from having to use any AJAX calls. –  patmortech Mar 21 '12 at 12:42

4 Answers 4

up vote 4 down vote accepted

The page is cached after the values like Context.User.Identity.IsAuthenticated are evaluated. So even though the JavaScript is executed on the client, it already contains the specific values (your values) from the cache.

To do this right, you can either

  1. remove those values from the script altogether, and perform an AJAX request to see who is logged in. But remember that the highlighting won't work at all if the browser has JavaScript disabled.
  2. cache the page per user
share|improve this answer
That makes sense. I don't know why it didn't occur to me that the evaluated Context.User.Identity values were also getting cached. I guess I'll have to go with per-user caching (which was the temporary solution I had used anyway, but I had been hoping I could take advantage of the rest of the page except for the highlight being identical), and switch back to doing the highlight in the ASP.NET code so it will work even if JavaScript is disabled. –  Blobinator Mar 21 '12 at 12:23

You can only cache the list items, not the user authentification. I'm not common to asp, so I can't tell you how to cache only parts of your site. But I'd try to either deliver the javascript as a separate page (with the same url for each user), where you can output the current username, referenced by the "static" list. Or you could try to make your js static, getting the username/authentification state from cookies which are stored individually for each user.

share|improve this answer

I do not have past experience in ASP.NET, but it seems to me that the caching mechanism just execute the code the first hit, generate the HTML, cache it, and provide that generated HTML to everyones that navigate to that page, until the cache is not expired.

It means: JS is executed properly – the row is highlighted – but the owner variable always contains the user's name of the first one that hit that page. You can easily check if it's the case, doing a "view source".

Say that, I don't think you can use this kind of cache mechanism for your scenario.

share|improve this answer

You could try using the Substitution control to insert the dynamic name of the logged in user into your cached page (that's what it's for).

To use it, first you would have to create a static method on your page class that will get called by the Substitution control and return a string containing the javascript you want to insert.

public static string GetCurrentUserInfo(HttpContext context) 
    bool isAuthenticated = context.User.Identity.IsAuthenticated;
    string owner = "";
    if (isAuthenticated) owner = context.User.Identity.Name;

    return string.Format("var isAuthenticated = {0}; var owner = '{1}';", isAuthenticated, owner);

Then you add the control inside your script tag:

<script type="text/javascript">
    <asp:Substitution ID="ownerInfo" runat="server" MethodName="GetCurrentUserInfo" />

Visual Studio won't like you putting the control there, but it will still function properly. Now you can use the isAuthenticated and owner variables in your jQuery method.

share|improve this answer
This is an interesting approach. Per-user caching seems to be working ok for this page, but I may try this approach on a couple of other pages where I have a similar situation (mostly static data with just a few different values depending on the user). –  Blobinator Mar 21 '12 at 16:47

Your Answer


