Serializing Object Properties: Web Controls vs. Components

Discussion in 'ASP .Net Building Controls' started by John Saunders, Sep 16, 2004.

  1. I know there's a simple way to do this, but I can't find it. A pointer to
    the answer I can't find will be great:

    I'm re-learning how to handle object properties of WebControls. I started by
    creating a Component with a SqlConnection property:

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data.SqlClient;
    using System.Diagnostics;

    namespace TypeConverterTests
    {
    public class ContainsConnection : System.ComponentModel.Component
    {
    // ... normal stuff

    private SqlConnection _connection;
    public SqlConnection Connection
    {
    get {return _connection; }
    set { _connection = value; }
    }
    }
    }

    I then create a blank web form. I drag an SqlConnection onto it and set the
    ConnectionString property. I then drag an instance of my ContainsConnection
    component onto the web page and set its Connection property to
    SqlConnection1. The dropdown works in the property grid, and I get the
    following code generated:

    //
    // containsConnection1
    //
    this.containsConnection1.Connection = this.sqlConnection1;

    As expected. I then created a Web Control with the exact same inner code as
    the ContainsControl component:

    public class ControlWithConnection : WebControl
    {
    public ControlWithConnection()
    {
    }

    private SqlConnection _connection;
    //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public SqlConnection Connection
    {
    get
    {
    return _connection;
    }
    set
    {
    _connection = value;
    }
    }
    }

    When I drag this onto the form and set the Connection property, I get the
    following in the .aspx file:

    <cc1:ControlWithConnection id="ControlWithConnection1" runat="server"
    Connection="sqlConnection1"></cc1:ControlWithConnection>

    This doesn't work too well at run-time, so I uncomment the
    DesignerSerializationVisibility attribute above, and get:

    <cc1:ControlWithConnection id="ControlWithConnection1"
    runat="server"></cc1:ControlWithConnection>

    which I'm surprised to see is just what I asked for!

    What I didn't want for is a total lack of my SqlConnection property being
    initialized in the codebehind!

    I have a vague recollection that I need to do something like implement a
    TypeConverter which can convert to an InstanceDescriptor and set that on the
    Connection property, but that doesn't make much sense to me in retrospect.
    It also doesn't work:

    public class SqlConnectionConverter : ReferenceConverter
    {
    public SqlConnectionConverter() :
    base(typeof(System.Data.SqlClient.SqlConnection))
    {
    }

    public override bool CanConvertTo(ITypeDescriptorContext context, Type
    destinationType)
    {
    if (destinationType.Equals(typeof(InstanceDescriptor)))
    {
    return true;
    }
    return base.CanConvertTo (context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context,
    System.Globalization.CultureInfo culture, object value, Type
    destinationType)
    {
    if (destinationType.Equals(typeof(InstanceDescriptor)))
    {
    ConstructorInfo ci = value.GetType().GetConstructor(Type.EmptyTypes);
    InstanceDescriptor x =
    new InstanceDescriptor(ci, new object[]{}, true);
    return x;
    }
    return base.ConvertTo (context, culture, value, destinationType);
    }
    }

    .... and of course, placing a TypeConverterAttribute on the Connection
    property. The dropdown works again, but nothing changes in the codebehind.

    I'm missing something, but I don't know what. Please help.

    John Saunders
     
    John Saunders, Sep 16, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mickey Segal
    Replies:
    0
    Views:
    899
    Mickey Segal
    Feb 2, 2004
  2. sonic
    Replies:
    0
    Views:
    349
    sonic
    Aug 24, 2006
  3. Ra Carter
    Replies:
    1
    Views:
    734
    sloan
    Mar 17, 2010
  4. Tom McDonnell

    Serializing properties as structured markup

    Tom McDonnell, Apr 21, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    118
    Tom McDonnell
    Apr 21, 2004
  5. sonic
    Replies:
    0
    Views:
    114
    sonic
    Aug 24, 2006
Loading...

Share This Page