ASP.NET Tutorial/Custom Controls/TypeConverter

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Applying the TypeConverter attribute to a property (C#)

   <source lang="csharp">

using System; using System.Collections.Generic; using System.ruponentModel; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace WebControlLibrary1 {

   [DefaultProperty("Text")]
   [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
   public class WebCustomControl1 : WebControl
   {
       private string text;
       private Guid _bookid;
       [Bindable(true)]
       [Category("Appearance")]
       [DefaultValue("")]
       public string Text
       {
           get
           {
               return text;
           }
           set
           {
               text = value;
           }
       }
       [Bindable(true)]
       [Category("Appearance")]
       [DefaultValue("")]
       [TypeConverter(typeof(GuidConverter))]
       public Guid BookId
       {
           get
           {
               return _bookid;
           }
           set
           {
               _bookid = value;
           }
       }
       protected override void Render(HtmlTextWriter output)
       {
           output.RenderBeginTag(HtmlTextWriterTag.Input);
           output.RenderEndTag();
       }
   }

}</source>


Applying the TypeConverter attribute to a property (VB)

   <source lang="csharp">

Imports System.ruponentModel Imports System.Web.UI <DefaultProperty("Text"), _

ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _

Public Class WebCustomControl1

   Inherits System.Web.UI.WebControls.WebControl
   Dim _text As String
   Dim _bookid As Guid
   <Bindable(True), Category("Appearance"), DefaultValue("")> _
   Property [Text]() As String
       Get
           Return _text
       End Get
       Set(ByVal Value As String)
           _text = Value
       End Set
   End Property
   <Bindable(True)> _
   <Category("Appearance")> _
   <DefaultValue("")> _
   <TypeConverter(GetType(GuidConverter))> _
   Property BookId() As System.Guid
       Get
           Return _bookid
       End Get
       Set(ByVal Value As System.Guid)
           _bookid = Value
       End Set
   End Property
   Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
       output.RenderBeginTag(HtmlTextWriterTag.Input)
       output.RenderEndTag()
   End Sub

End Class</source>


Creating a custom type converter (C#)

   <source lang="csharp">

using System; using System.ruponentModel; using System.Globalization; public class Name {

   private string _first;
   private string _last;
   public Name(string first, string last)
   {
       _first = first;
       _last = last;
   }
   public string First
   {
       get { return _first; }
       set { _first = value; }
   }
   public string Last
   {
       get { return _last; }
       set { _last = value; }
   }

} public class NameConverter : TypeConverter {

   public override bool CanConvertFrom(ITypeDescriptorContext context,
      Type sourceType)
   {
       if (sourceType == typeof(string))
       {
           return true;
       }
       return base.CanConvertFrom(context, sourceType);
   }
   public override object ConvertFrom(ITypeDescriptorContext context,
      CultureInfo culture, object value)
   {
       if (value is string)
       {
           string[] v = ((string)value).Split(new char[] { " " });
           return new Name(v[0], v[1]);
       }
       return base.ConvertFrom(context, culture, value);
   }
   public override object ConvertTo(ITypeDescriptorContext context,
      CultureInfo culture, object value, Type destinationType)
   {
       if (destinationType == typeof(string))
       {
           return ((Name)value).First + " " + ((Name)value).Last;
       }
       return base.ConvertTo(context, culture, value, destinationType);
   }

}</source>


Creating a custom type converter (VB)

   <source lang="csharp">

Imports System Imports System.ruponentModel Imports System.Globalization Public Class Name

   Private _first As String
   Private _last As String
   Public Sub New(ByVal first As String, ByVal last As String)
       _first = first
       _last = last
   End Sub
   Public Property First() As String
       Get
           Return _first
       End Get
       Set(ByVal value As String)
           _first = value
       End Set
   End Property
   Public Property Last() As String
       Get
           Return _last
       End Get
       Set(ByVal value As String)
           _last = value
       End Set
   End Property

End Class Public Class NameConverter

   Inherits TypeConverter
   Public Overrides Function CanConvertFrom(ByVal context As _
      ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
       If (sourceType Is GetType(String)) Then
           Return True
       End If
       Return MyBase.CanConvertFrom(context, sourceType)
   End Function
   Public Overrides Function ConvertFrom( _
           ByVal context As ITypeDescriptorContext, _
           ByVal culture As CultureInfo, ByVal value As Object) As Object
       If (value Is GetType(String)) Then
           Dim v As String() = (CStr(value).Split(New [Char]() {" "c}))
           Return New Name(v(0), v(1))
       End If
       Return MyBase.ConvertFrom(context, culture, value)
   End Function
   Public Overrides Function ConvertTo( _
           ByVal context As ITypeDescriptorContext, _
           ByVal culture As CultureInfo, ByVal value As Object, _
           ByVal destinationType As Type) As Object
       If (destinationType Is GetType(String)) Then
           Return (CType(value, Name).First + " " + (CType(value, Name).Last))
       End If
       Return MyBase.ConvertTo(context, culture, value, destinationType)
   End Function

End Class</source>