I found another thread adressing the problem.
This is the sourcecode i finally ended up with. It is not perfect yet (and a
bit strange), but I hope that I will be able to fix the last issues.
It is possible to exhcange "Control" with any other control, webcontrol, or
homemade control.
Jørn Esbensen
-----------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;
using System.Collections;
using System.IO;
namespace KommuneInformation.WebComponents.NIS
{
/// <summary>
/// Summary description for NISTest.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:NISTest runat=server></{0}:NISTest>")]
[Designer(typeof(NISTestDesigner))]
public class NISTest :
System.Web.UI.WebControls.WebControl,INamingContainer
{
private string text;
private Control myComponent=null;
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Control testComponent
{
get
{
return myComponent;
}
set
{
myComponent = value;
}
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
DataBind();
output.Write(DateTime.Now.ToString() + ";");
if (myComponent!=null)
output.Write("NOTNULL;");
else
output.Write("NULL;");
output.Write(text);
}
}
public class NISTestDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
NISTest obj=(NISTest)this.Component;
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
obj.RenderControl(tw);
return sw.ToString();
}
public string testComponent
{
get
{
DataBinding binding = DataBindings["testComponent"];
if (binding != null)
{
return binding.Expression;
}
return String.Empty;
}
set
{
if ((value == null) || (value.Length == 0))
{
DataBindings.Remove("testComponent");
}
else
{
DataBinding binding = DataBindings["testComponent"];
if (binding == null)
{
binding = new DataBinding("testComponent", typeof(Control), value);
}
else
{
binding.Expression = value;
}
DataBindings.Add(binding);
}
OnBindingsCollectionChanged("testComponent");
IContainer container = (IContainer)this.GetService(typeof(IContainer));
((NISTest)Component).testComponent =
(Control)container.Components[value];
}
}
protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);
PropertyDescriptor prop;
prop = (PropertyDescriptor)properties["testComponent"];
System.ComponentModel.AttributeCollection runtimeAttributes =
prop.Attributes;
Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
runtimeAttributes.CopyTo(attrs, 0);
attrs[runtimeAttributes.Count] = new
TypeConverterAttribute(typeof(NISTestComponentConverter));
prop = TypeDescriptor.CreateProperty(this.GetType(), "testComponent",
typeof(string),attrs);
properties["testComponent"] = prop;
}
}
internal class NISTestComponentConverter : TypeConverter
{
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
object[] components = null;
if (context != null)
{
ArrayList list = new ArrayList();
list.Add(null);
IContainer cont = context.Container;
if (cont != null)
{
ComponentCollection objs = cont.Components;
foreach(IComponent obj in objs)
if (obj is Control)
list.Add(obj.Site.Name);
}
components = list.ToArray();
}
return new StandardValuesCollection(components);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext
context)
{
return true;
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext
context)
{
return true;
}
}
}