views:

37

answers:

3

Hello,

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?

+1  A: 

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...

Kendrick
A: 

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.

Shiraz Bhaiji
I set the value inside if(!IsPostback) { ... } and all the other request on the page goes through ajax.net. I will do that quick fix tho, but if feels kind of scary that im loosing some of the viewstate.
Jimmy
A: 

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

citronas