Solved

How can I serialize an ASP.NET User Control?

Posted on 2006-10-27
3
2,705 Views
Last Modified: 2010-04-30
I am programmatically adding user controls from an .ascx file into my application on Page_Load, which is necessary because the controls are populated with a database query and the query may return the need for any number of controls to be added.  I want to maintain these controls in the ViewState so that on post-back I can re-add the objects while maintaining the updated values from any changes a user might have made (selecting an item from a drop-down, for example).  When I try to add these controls to the ViewState, however, I get the following exception, which basically tells me that the Serializable attribute is not applied to my control's class:
System.Runtime.Serialization.SerializationException: Type 'ASP.MyClass' in Assembly 'App_Web_pf3y4e1h, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

If this were a standard vb class, then I would simply apply the <Serializable()> attribute just before the class declaration, but here the class declaration is implicit in the first line of the file:  
<%@ Control Language="VB" ClassName="MyClass" %>

I have accomplished my ultimate goal here before by manually creating a hashtable object with each of the properties of the control, and then manually re-adding the controls on post-back.  This, however, seems inefficient, since I already have an object to house this data.  How can I apply the Serializable attribute to my control?
0
Comment
Question by:BofADev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
jnhorst earned 125 total points
ID: 17822484
Have you tried putting the <Serializable()> attribute in front of the class delcaration for the UserControl(s) (in their codebehind)?

John
0
 

Author Comment

by:BofADev
ID: 17906085
thanks for helping me think outside the box.  i typically try to avoid code-behinds, as they only make more files and more complexity.  however, this is a case when they're necessary, i suppose.
0
 

Expert Comment

by:cyclides
ID: 22531870
Try to use <Serializable()> for the class and for the properties:
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)>

Omar
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question