Solved

How can I serialize an ASP.NET User Control?

Posted on 2006-10-27
3
2,679 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
Comment Utility
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
Comment Utility
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
Comment Utility
Try to use <Serializable()> for the class and for the properties:
<System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)>

Omar
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

11 Experts available now in Live!

Get 1:1 Help Now