Вопрос

When I try to initialize ViewState key with value of type Group class it throws the following exception:

exception: Exception of type 'System.Web.HttpUnhandledException' was thrown.Error serializing value 'MMS.Classes.DBEntities.Group' of type 'MMS.Cl.Entities.Group.'

code:

protected void Page_PreRender(object sender, EventArgs e)
        {
            Controls();
        }

        private void Controls()
        {
            if (!Page.IsPostBack)
            {
                ViewState["Group"] = new Group();
                ViewState["Group"] = new List<string>(); // if I use this line instead the above it works works
            }
        }
Это было полезно?

Решение

I think simply marking your Group class as serializable would make your code work, as the ViewState needs the class to be serializable to store it. For details about serialization you can visit this link. Add [Serializable] attribute to your class as following:

[Serializable]
public class Group
{
//properties
}

Hope this helps.

Другие советы

since the result of storing items in ViewState is that those are stored in string on page (to maintain state between multiple requests), they need to be serializable, on the other hand Session is in memory storage (if you're using InProc mode, which is default, otherwise this doesn't apply), so objects don't need to be serializable.

Refer to this for more details:

Why Viewstate can contain only serializable object?

Updated:

You can find more information about Serialization here:

http://msdn.microsoft.com/en-us/library/ms233843.aspx

Hope this helps .

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top