Вопрос

I am trying to store some temporary list data so I can let the user edit them before save to database.

public List<ScheduleEntry> NewScheduleEntry
{
    get
    {
        String PersistentName = "List_ScheduleEntry";
        if (ViewState[PersistentName] == null || !(ViewState[PersistentName] is List<ScheduleEntry>))
        {
            ViewState[PersistentName] = new List<ScheduleEntry>();
        }
        return ViewState[PersistentName] as List<ScheduleEntry>;
    }
}

public List<ScheduleEntry> ListView_CourseScheduleEntry_GetData()
{
    return NewScheduleEntry;
}

This is not the first time I use this technic but it is not working. There is no exception and I can see ListView_CourseScheduleEntry_GetData was run until return statement.

But if I change the ViewState to Session (no other change), it works fine. Unfortunately I should not use session here, since it is a page transaction.

Is it possible that the viewstate's Base64 encoding string was broken by the list data?

Это было полезно?

Решение

In contrast to values stored in Session Memory, classes that are stored in ViewState need to be marked as Serializable as the ViewState is serialized to the page in any case (as long as Session Memory is held on the server, the objects are stored in memory without serialization). This explains why it works when objects are stored in Session and doesn't work when stored in ViewState.

Therefore, adding the Serializable attribute to the ScheduleEntry class and all related classes should solve the issue:

[Serializable]
public class ScheduleEntry
{
    // ...
}

For details on ASP.NET ViewState see this link.

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