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

Source: Internet
Author: User
Tags bool

In the previous viewstate--Custom state management (i) I added an overridden ToString method to the class of custom attributes and a constructor that gets an instance of the type from a string. You might think that talking about the state management of custom attributes doesn't mention TypeConverter, something magical. Well, let's talk about TypeConverter.

Take a look at the explanation for MSDN: Provides a uniform way to convert a type of value to another type and to access standard values and child properties.

In the last article I wrote two methods without using TypeConverter is to explain the role of TypeConverter on the one hand (from this point is type conversion). Of course there are many other uses for TypeConverter, for example, to provide a set of standard values. So in general we use TypeConverter to do this (even if it's just a problem with type conversions). On a personal understanding, TypeConverter is what we use when the property form enters a value for a control property and assigns a value to a control property in an ASPX or Ascx declaration control, it controls how our input is converted to a type corresponding to a control property. You might say that we can actually call it to do something, yes, that's OK.

First, revise the previous example:

ViewStateManagement4

Namespace Controls {using System;
  Using System.Collections.Generic;
  Using System.Text;
  Using System.Web.UI;
  Using System.ComponentModel;
  
  Using System.Globalization;
    public class Viewstatemanagement4:control {private CustomProperty4 m_property;
      [DesignerSerializationVisibility (Designerserializationvisibility.content)] public CustomProperty4 Property {
        get {if (M_property = = null) {M_property = new CustomProperty4 ();
      return m_property; //set//{//}} protected override void LoadViewState (object savedstate) {if (saved
        State is Pair) {Pair properties = (Pair) savedstate; Base. LoadViewState (properties.
        a); M_property = (CustomProperty4) properties.
      Second; } else {base.
      LoadViewState (savedstate); } protected Override Object SaveViewState () {Object basestate = BasE.saveviewstate ();
      if (property!= null) {Return to New Pair (Basestate, property);
    return basestate; } protected override void TrackViewState () {base.
    TrackViewState (); } protected override void Render (HtmlTextWriter writer) {//customtypeconverter c = new Customtypeconvert
      ER ();
      C.convertto (null, CultureInfo.InvariantCulture, property, typeof (String)); Writer.
  
      Write (C.convertto (null, CultureInfo.InvariantCulture, property, typeof (String)); Writer. Write (Typedescriptor.getconverter (Property.gettype ()).
    ConvertTo (null, CultureInfo.InvariantCulture, property, typeof (String));
    } [TypeConverter (typeof (Customtypeconverter))] public class CustomProperty4 {private string m_property1;
  
    private string M_property2;
      public string Property2 {get {return m_property2;}
    set {M_property2 = value;} public string Property1 {get {return m_Property1;
    set {m_property1 = value;} The public class Customtypeconverter:expandableobjectconverter {/**////<summary>///Specifies whether you can select from a specific Type conversion///</summary>///<param name= "context" ></param>///<param name= "SourceType" > </param>///<returns>true: can; false: cannot </returns> public override bool CanConvertFrom (ITYPEDESCR Iptorcontext context, type sourcetype) {//If the source type is a string if (sourcetype = = typeof (String)) {RET
      Urn true;
    return false; /**////<summary>///Specifies whether you can convert to a specific type///</summary>///<param name= "context" ></para m>///<param name= "destinationtype" ></param>///<returns></returns> public Overri de bool CanConvertTo (ITypeDescriptorContext context, Type destinationtype) {if (destinationtype = = typeof (Strin
      g)) {return true;
  
     } return false; 
    /**////<summary>///Conversion from a specific value///</summary>///<param name= "context" ></param> <param name= "Culture" ></param>///<param name= "value" ></param>///&LT;RETURNS&G T;</returns> public override Object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value {if (value is string) {string str = (str
        ing) value; string[] propertyvalues = str.
        Split (';');
          if (propertyvalues.length = = 2) {CustomProperty4 obj = new CustomProperty4 (); Obj.
          Property1 = Propertyvalues[0]; Obj.
          Property2 = propertyvalues[1];
        return obj; Return base.
    ConvertFrom (context, culture, value);
    /**////<summary>///converted to a specific value///</summary>///<param name= "context" ></param> <param name= "Culture" &GT;&LT;/PARAM&GT <param name= "value" ></param>///<param name= "destinationtype" ></param>///<return S></returns> public override Object ConvertTo (ITypeDescriptorContext context,
      System.Globalization.CultureInfo culture, Object value, Type destinationtype) {if (value is CustomProperty4)
        {CustomProperty4 obj = (CustomProperty4) value; return obj. Property1 + ";" + obj.
      Property2; Return to base.
    ConvertTo (context, culture, value, destinationtype); }   } }

Related Article

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.