Hej, jeg har et irriterende problem med Collection Properties.
I mit tilfælde har jeg brug for at have en collection property i "hovedklassen" og i hver af objekterne skal der være endnu en collection property.
Dvs en BanditItemCollection i WebControlen der består af MyThingy-objekter.
I MyThingy-objektet har jeg så MyNanoCollection der består af MyNano-objekter.
Starter med sådan set at virke fint, med output i aspx'en.
f.eks.
<cc4:WebCustomControl1 ID="WebCustomControl1_1" runat="server" BanditItemCollection-Capacity="4">
<MyThingy title="abc" amount="123" tjuhej="">
<MyNanoCollection>
<MyNano id="1" name="a" someothervalue="b" />
<MyNano id="2" name="c" someothervalue="d" />
</MyNanoCollection>
</MyThingy>
<MyThingy title="def" amount="456" tjuhej="">
<MyNanoCollection>
</MyNanoCollection>
</MyThingy>
</cc4:WebCustomControl1>
Nå men, ved skift tilbage til designtime får jeg en fejl på kontrollen..
"WebControlLibrary1.MyNanoCollection must have items of type 'WebControlLibrary1.MyNano'. 'MyNano' is of type 'System.Web.UI.HtmlControls.HtmlGenericControl'.
Nogle gode ideer?
-----------------------
[DefaultProperty("Text")
,ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")
,ParseChildren(false)
,PersistChildren(false)
,Designer(typeof(HtmlDesigner))
,ControlBuilder(typeof(HtmlBuilder))]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected MyItemCollection _myItemCollection=null;
[
Bindable(true)
,Category("Appearance")
,Browsable(true)
,PersistenceMode(PersistenceMode.InnerDefaultProperty)
,DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
]
public MyItemCollection BanditItemCollection
{
get
{
if (_myItemCollection == null)
_myItemCollection = new MyItemCollection();
return _myItemCollection;
}
set
{
_myItemCollection = value;
}
}
protected override void Render(HtmlTextWriter output)
{
output.Write("Ostehat");
}
protected override void AddParsedSubObject(object obj)
{
if (obj is MyThingy)
{
MyThingy h = obj as MyThingy;
this.BanditItemCollection.Add(new MyThingy(h.Amount, h.Title, h.Tjuhej));
}
else
{
base.AddParsedSubObject(obj);
}
}
}
-----------------------------------------------
public class MyNano
{
private int id;
private string name;
private string someothervalue;
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public string SomeOthervalue
{
get { return this.someothervalue; }
set { this.someothervalue = value; }
}
public MyNano()
{
}
public MyNano(int id,string name,string someothervalue)
{
this.id = id;
this.name = name;
this.someothervalue = someothervalue;
}
}
[ParseChildren(false)
,PersistChildren(false)
,Designer(typeof(HtmlDesigner))
,ControlBuilder(typeof(HtmlBuilder))]
public class MyThingy
{
private string sTitle;
private int iAmount=0;
private string sTjuhej = "";
public string Tjuhej
{
get { return sTjuhej; }
set { sTjuhej = value; }
}
public string Title
{
get { return sTitle; }
set { sTitle = value; }
}
public int Amount
{
get {return iAmount;}
set {iAmount=value;}
}
protected MyNanoCollection _myNanoCollection = null;
[
Bindable(true)
, Category("Appearance")
, Browsable(true)
, PersistenceMode(PersistenceMode.InnerDefaultProperty)
, DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
, Description("Returns the collection of colors to list as options when"
+ " the Palette property is set to UserDefined")
]
public MyNanoCollection MyNanoCollection
{
get
{
if (_myNanoCollection == null)
_myNanoCollection = new MyNanoCollection();
return _myNanoCollection;
}
set
{
_myNanoCollection = value;
}
}
public MyThingy()
{
}
public MyThingy(int aAmount,string aTitle,string aTjuhej)
{
iAmount = aAmount;
sTitle = aTitle;
}
}
public class MyNanoCollection : CollectionBase
{
public int Add(MyNano aItem)
{
return List.Add(aItem);
}
public void Insert(int aIndex, MyNano aItem)
{
List.Insert(aIndex, aItem);
}
public void Remove(MyNano aItem)
{
List.Remove(aItem);
}
public bool Contains(MyNano aItem)
{
return List.Contains(aItem);
}
public int IndexOf(MyNano aItem)
{
return List.IndexOf(aItem);
}
public void CopyTo(MyNano[] aArray, int aIndex)
{
List.CopyTo(aArray, aIndex);
}
public MyNano this[int aIndex]
{
get { return (MyNano)List[aIndex]; }
set { List[aIndex] = value; }
}
}
public class MyItemCollection : CollectionBase
{
public int Add(MyThingy aItem)
{
return List.Add(aItem);
}
public void Insert(int aIndex, MyThingy aItem)
{
List.Insert(aIndex, aItem);
}
public void Remove(MyThingy aItem)
{
List.Remove(aItem);
}
public bool Contains(MyThingy aItem)
{
return List.Contains(aItem);
}
public int IndexOf(MyThingy aItem)
{
return List.IndexOf(aItem);
}
public void CopyTo(MyThingy[] aArray, int aIndex)
{
List.CopyTo(aArray, aIndex);
}
public MyThingy this[int aIndex]
{
get { return (MyThingy)List[aIndex]; }
set { List[aIndex] = value; }
}
}
-----------------------------------------------------
public class HtmlDesigner : ControlDesigner
{
public override string GetPersistInnerHtml()
{
StringWriter sw = new StringWriter();
HtmlTextWriter html = new HtmlTextWriter(sw);
WebCustomControl1 dd = this.Component as WebCustomControl1;
if (dd!=null)
{
foreach (MyThingy c in dd.BanditItemCollection)
{
html.WriteBeginTag("MyThingy");
html.WriteAttribute("title",c.Title);
html.WriteAttribute("amount", c.Amount.ToString());
html.WriteAttribute("tjuhej", c.Tjuhej);
html.WriteLine(HtmlTextWriter.TagRightChar);
html.WriteBeginTag("MyNanoCollection");
html.WriteLine(HtmlTextWriter.TagRightChar);/**/
foreach (MyNano nano in c.MyNanoCollection)
{
html.WriteBeginTag("MyNano");
html.WriteAttribute("id", nano.ID.ToString());
html.WriteAttribute("name", nano.Name);
html.WriteAttribute("someothervalue", nano.SomeOthervalue);
html.WriteLine(HtmlTextWriter.SelfClosingTagEnd);
}
html.WriteEndTag("MyNanoCollection");
html.WriteEndTag("MyThingy");
}
}
return sw.ToString();/**/
//return "GetPersistInnerHtml";
}
}
--------------------------------------------------------
public class HtmlBuilder : ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary attribs)
{
if (string.Compare(tagName, "MyThingy", true) == 0)
return typeof(MyThingy);
if (string.Compare(tagName, "MyNano", true) == 0)
return typeof(MyNano);
if (string.Compare(tagName, "MyNanoCollection", true) == 0)
return typeof(MyNanoCollection);
return base.GetChildControlType(tagName, attribs);
}
}
Indlæg senest redigeret d. 24.04.2006 16:56 af Bruger #7110