Serializing Object Properties: Web Controls vs. Components

J

John Saunders

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,821
Messages
2,569,725
Members
45,511
Latest member
Osiris-Team

Latest Threads

Top