views:

40

answers:

2

My asp.net application has a function that returns the HTML for the navigation menu for the user by getting it from a database

currently, I am storing the text in a session variable when the session begins and then use it to set the innerHtml of the navigation div on the on_load method.

The problem is that the pages now contain the

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPD..

with the value being 7000 characters long.

Is there any better way to do this or a different way to store and retrieve values without them being stored in the viewstate ?

The code is just this:

Session["menuHTML"] = (new NavMenu().GetMenuHTML());
navMenuDiv.InnerHtml = Session["menuHTML"].ToString();

The div is declared as

<div id="navMenuDiv" class="navMenuDiv" runat="server"></div>
+1  A: 

I would cache the data returned from your database call and then generate the navigation html for each page as it's the db call that is the bit you don't want to be doing over and over.

To cache the data I'd do something like this (have not tried it):

public NavData GetNavData()
{
    NavData navdata = Cache["NavData"];
    if (navdata == null)
    {
        navdata = SomeDataStore.GetNavDataFromDatabase();
        Cache["NavData"] = navdata;
    }
    return navdata;
}

In answer to your question though, and without being rude, are you sure you're putting it in Session and not ViewState?

EDIT: Because you have turned that div into a server control, it's storing it's value in ViewState.

Switch ViewState off for navMenuDiv:

navMenuDiv.EnableViewState = false;
DavidGouge
yep. This is what i have in my session_start code. Session["menuHTML"] = (new NavMenu().GetMenuHTML())
TP
and how would you "cache the data"?
TP
Edited answer to show example of caching the data.
DavidGouge
Will the cache be different for each user who uses the application? Because the menu generated is based on the privilege level of the user
TP
Ah, no. In that case the data needs to go into session which brings us neatly round to your original problem.
DavidGouge
Could you show your code that populates the nav?
DavidGouge
edited to show the code. It is a recursive function. The recursive call is made in the menuCode.appendFormat area
TP
Ok, what I meant is the code that puts the result of that method onto your page. What I *think* might be happening is that whatever control you're pumping the nav html into is storing its value in viewstate.
DavidGouge
edited to show that code.
TP
Ahh, think I've got it. Edited answer.
DavidGouge
Thanks.....................
TP
+1  A: 

Use EnableViewState=”false” in your div:

<div id="navMenuDiv" class="navMenuDiv" runat="server" EnableViewState=”false”></div>
Geert Immerzeel