Jimmy
Jimmy

Reputation: 393

Losing the viewstate

I'm storing a few of my properties in the viewstate, so I can use them easily on Ajax requests. My property code looks like this:

public Language Language
{
    get { return (Language)ViewState["controls_window_Language"]; }
    set { ViewState["controls_window_Language"] = value; }
}

However, my customers have reported some errors, and when I've tracked it down, it's because Language is null. It doesn't happen every time; it appears to be totally random, and I can't reproduce the error. I'm also storing other properties inside the viewstate, and I'm using that property just before Language, so I havn't lost all viewstate.

Most logical reason would be that Language is overwritten, but the only time I write to it is when the page is first loaded.

What can be the reason for losing my viewstate property?

Upvotes: 2

Views: 1794

Answers (5)

Alexandre
Alexandre

Reputation: 1232

One possible reason for "losing" ViewState content is Output Cache. I'm facing the same problem and the cause is caching (when I disable it, the problem doesn't occur).

Upvotes: 0

citronas
citronas

Reputation: 19365

Try to see if there is a connection between the Session_OnError Event in the global.asax and your ViewState problem

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

You say that you write to the viewstate when the page is first loaded.

How do you know that it is the first time the page is loaded, is there a navigation route in your app that could by pass the setting.

As a quick fix, you could try checking if the value is null and then returning a default value.

Upvotes: 0

Kendrick
Kendrick

Reputation: 3787

I'm not sure if this is the issue, but using the back/forward navigation in the browser can often cause unexpected results, especially on pages using a lot of asynchronous calls.

Edit: to clarify my thinking...

I'm suggesting this might be why users are seeing the error but you can't reproduce the problem. This is one step in troubleshooting I often forget about...

Upvotes: 1

Related Questions