Solved

How can I serialize an ASP.NET User Control?

Posted on 2006-10-27
3
2,695 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now