How to store a structure in ViewState

  • Thread starter Thread starter george d lake
  • Start date Start date
G

george d lake

Hi, I have a structure that I need to save between pages. Can not use
sessions. I would love to use ViewState, but, I get this error!

The type 'TicketSystemV2.ucTicketList+OrderBy' must be marked as
Serializable or have a TypeConverter other than ReferenceConverter to be put
in viewstate.


Here is my code:

Public Structure OrderBy
Public DateSort As Boolean
Public SortDir As String
Public SeveritySort As Boolean
Public AssignedToSort As Boolean
Public OfficeSort As Boolean
Public ProblemSort As Boolean
Public TimeSort As Boolean
End Structure

In the page load:

Dim pOrderBy As New OrderBy()

pOrderBy.DateSort = True
pOrderBy.SortDir = "D"
pOrderBy.SeveritySort = False
pOrderBy.AssignedToSort = False
pOrderBy.OfficeSort = False
pOrderBy.ProblemSort = False
pOrderBy.TimeSort = False

ViewState.Add("bla", pOrderBy)


The error message happens once all the code has been executed and the page
is acutally loading.

Any ideas?
 
Viewstate doesn't support holding objects like that, at least not that
easily. If you want to keep the solution simple, make two functions. The
first one takes in this structure as a parameter, and returns a string (a
string which somehow contains enough info about the structure to recreate
it) For example "t;mysordir;t;f;t;f;f". Store that string to the
viewstate. In your second method, take in this string (which you retrieve
from the viewstate), do whatever you need to recreate the structure, and
return the structure.

Sounds good to me anyway :-)
--Michael
 
yea.... I had that idea too.

looks like thats the way to go.....


Thanks for the help.

George
 
In green:

Hi, I have a structure that I need to save between pages. Can not use
sessions. I would love to use ViewState, but, I get this error!

The type 'TicketSystemV2.ucTicketList+OrderBy' must be marked as
Serializable or have a TypeConverter other than ReferenceConverter to be put
in viewstate.


Here is my code:

<Serializable()> _
Public Structure OrderBy
Public DateSort As Boolean
Public SortDir As String
Public SeveritySort As Boolean
Public AssignedToSort As Boolean
Public OfficeSort As Boolean
Public ProblemSort As Boolean
Public TimeSort As Boolean
End Structure

In the page load:

Dim pOrderBy As New OrderBy()

pOrderBy.DateSort = True
pOrderBy.SortDir = "D"
pOrderBy.SeveritySort = False
pOrderBy.AssignedToSort = False
pOrderBy.OfficeSort = False
pOrderBy.ProblemSort = False
pOrderBy.TimeSort = False

ViewState.Add("bla", pOrderBy)


The error message happens once all the code has been executed and the page
is acutally loading.

Any ideas?
 
Back
Top