0

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?

2
  • 2
    Is the ScheduleEntry class marked as Serializable? Do you also add some items to the list? Commented Apr 27, 2014 at 6:45
  • @Markus You are right. Actually ScheduleEntry is from EF and I have to mark [Serializable] for all related class..... Commented Apr 27, 2014 at 12:13

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

[Serializable] for all related class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.