asp.net viewstate--Custom state management (i)

Source: Internet
Author: User

When writing your own controls, we can't always just define properties for built-in types such as String,int--night roads are always going to hit ghosts. How do we save view state when complex attributes (custom types) are encountered? This problem has been bothering me for a long time ...

I would like to use a few small examples to express their "ghost" experience.

Husband

Namespace Controls
{
using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web.UI;
public class Husband:control
{public
Wife mywife
{
get
{
. 
  
   }
set
{
viewstate["mywife"] = value;
}} public
class Wife
{
private string m_name;
private int m_age;
public string Name
{get
{m_name;}
set {m_name= value;}
Public
int age
{get
{m_age;}
set {m_age= value;}
}
}

  

See this code, a lot of friends are expected to scold me, why, run under: found that will throw the following exception

Type ' Controls.wife ' in Assembly ' Controls, version=1.0.0.0, culture=neutral, Publickeytoken=null ' are not marked as serial Izable.

After seeing this anomaly, I used to add [Serializable]attribute to the front of the wife, plus it did run successfully but unfortunately, it was a way to get a brick.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.