How to use another control as a property

J

Jørn Esbensen

I am developing a set of webcontrols that are supposed to work together,
which means that I want them to reference each other, send data between each
other etc.

Therefore I have made a property i a given control to hold the control I
want to interact with.

As long as I only use the property at run-time, sets it in the code it works
perfectly.
When I set the property at design-time it seems to work, but when I save and
compile the project, the property gets "reset" to nothing.

How can I make sure that the settings is persisted like regular properties
(strings and numbers)?
I am using c#

Jørn Esbensen
 
T

Teemu Keiski

Hi,

please show bit of the code how you have specified the properties? I have a
hunch that exposing complete control instance via property might bring
problems at design-time, but if you create top-level properties that map to
child control proeprties, it should work.
 
J

Jørn Esbensen

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;
}
}

}
 

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
474,125
Messages
2,570,748
Members
47,301
Latest member
SusannaCgx

Latest Threads

Top