Dynamic text being stored in viewstate
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()开发者_JAVA技巧.GetMenuHTML());
navMenuDiv.InnerHtml = Session["menuHTML"].ToString();
The div is declared as
<div id="navMenuDiv" class="navMenuDiv" runat="server"></div>
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;
Use EnableViewState=”false” in your div:
<div id="navMenuDiv" class="navMenuDiv" runat="server" EnableViewState=”false”></div>
精彩评论