Solved

How can I serialize an ASP.NET User Control?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
@Html.EditorFor displays mm/dd/yyyy rather than date 2 34
Jquery autocomplete 10 68
Entity Framework 7 32
How to make a GridView cell hyperlinked using C# ? 3 17
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

15 Experts available now in Live!

Get 1:1 Help Now