How do I add a collection e.g. dynamic array as a property of a custom asp.net control?
I'm trying to add a dynamically expandable property to a composite control that I can drop on the designer surface. I've tried this with a String Array, a List, and an ArrayList. All with similar results. I'm missing something and I don't know what. Here is what I think is the relevant code:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
开发者_如何学C PersistenceMode(PersistenceMode.InnerProperty)]
public String[] AccessLevels
{
get
{
String[] s = (String[])ViewState["AccessLevels"];
return s;
}
set
{
ViewState["AccessLevels"] = value;
}
}
The control compiles fine and I can drop it on my designer surface. It gives me a property in the "Properties" window on Visual Studio 2008 called "+AccessLevels" with a value of "String[] Array" and an elipsis [...] next to it. If I click the elipsis it opens up an editor where I can insert properties one line at a time. If I click the little plus symbol next to the "AccessLevels" property, it shows the properties that I entered and each has a number next to it, indicating the index of the array. Great! But when I do this, regardless of what values I enter as properties, the following HTML is autogenerated by the designer.
<cc2:HBAdmin ID="HBAdmin1" runat="server">
<AccessLevels>
<system.string></system.string>
<system.string></system.string>
<system.string></system.string>
</AccessLevels>
</cc2:HBAdmin>
HBAdmin is the name of my control, and the cc2 namespace is correct. The problem is the tags. There should be a value in there right? I also get intellisense for the <AccessLevels> tag telling me "Content is not allowed between the opening and closing tags for element 'AccessLevels'", and also a validation error on the <system.string> tag telling me "Element 'System.String' is not supported. Then if I try to view the page with the control in the browser (after editing the properties in the design window that results in the markup above) I get the following error on the line with the opening tag for the control:
"Array creation must have array size or array initializer"
I only get this error when I try to edit the properties in the properties window of the designer. I can populate the property with values in the constructor and the page with the control will load in the browser and work fine, and the values show up fine in the properties window of the design surface and I can use the values throughout the control, but I can't edit them in the properties window. If I try to add one I get the same symptoms as described above.
I feel like I'm probably just missing some sort of attribute or declaration or something. Please help?
You can use Collection<string>
instead of string[]
Try something like:
Usage:
<cc:SomeClass >
<CustomLavel key="" value="" />
<CustomLavel key="" value="" />
<CustomLavel key="" value="" />
</cc:SomeClass>
public class SomeClass: Control, INamingContainer
{
private Collection<CustomLabel> _customLabelList;
protected override void AddParsedSubObject(object obj)
{
base.AddParsedSubObject(obj);
if (obj is CustomLabel)
{
_customLabelList.Add((CustomLabel)obj);
return;
}
}
[Category("Behavior")]
[Description("The fields collection")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
[DefaultValue(null), MergableProperty(false), Bindable(false)]
public Collection<CustomLabel> CustomLabelList
{
get
{
return _customLabelList;
}
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomLabel
{
private string _key;
private string _value;
public CustomLabel()
: this(string.Empty, string.Empty)
{
}
public CustomLabel(string key, string value)
{
_key = key;
_value = value;
}
[Category("Behavior")]
[DefaultValue("")]
[Description("Key")]
[NotifyParentProperty(true)]
public string Key
{
get
{
return _key;
}
set
{
_key = value;
}
}
[Category("Behavior")]
[DefaultValue("")]
[Description("Value")]
[NotifyParentProperty(true)]
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
}
精彩评论