質問

I am attempting to store a usercontrol in session state between postbacks, using SQL as stateserver.

I get the class not serializable error:

Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.

Stack Trace

SerializationException: Type 'ASP.bookingcontrols_controls_ucDates_ascx' in Assembly 'App_Web_eckjngpu, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.]

Now, marking the usercontorl class as Serializable, I still get this error. From research I have determined, thus far, that a usercontrol cannot be serialized. Fine.

The general proposed idea to get around this, seems to be to split control (data) from the control (interface/front end), and rebuild it on the other side.

Now, before I attempt to make some impacting changes to my code, my question is the following:

Will an object derived from the usercontrol class, but not created as a dynamic usercontrol by means of page.loadcontrol("control.ascx"), be serializable, or do I have to create a seperate "accompanying class" to store the data. If so, I assume I can not inherit from the control class to get the properties, as it will ultimately inherit the usercontrol classes that are not serializable?

Example:

Not using:

Dim myDatesControl As New UserControl
myDatesControl = Page.LoadControl("~/bookingControls/ucDates.ascx")

        With CType(myDatesControl, controls_ucDates)
            .checkInDate = Session("nowcheckin")
            .checkOutDate = Session("nowcheckout")
            .Nights = "xxx"
            .guid = currentBookingFilter.guid
            .ExtraInfo = currentBookingFilter
        End With

Would the following work, as an object that would be serializable to out of process session state.

Dim myDatesControl As New controls_ucDates

        With CType(myDatesControl, controls_ucDates)
            .checkInDate = Session("nowcheckin")
            .checkOutDate = Session("nowcheckout")
            .Nights = "xxx"
            .guid = currentBookingFilter.guid
            .ExtraInfo = currentBookingFilter
        End With

In the second block, would myDatesControl be serializable?

役に立ちましたか?

解決

nThe end result was that the myDatesContorl class, inheriting from usercontrol class, could not be serialized to session state.

I created a "accompanying" class and duplicated the properties of the UC. In my code, I then use the accompanying class to store state about the control, and create the controls only before display, re-reassign their properties.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top