I have a custom control with a ListItemCollect ion. If I add items by
hand in the aspx and switch to design time view, everything works fine.
However if I add items through the Collection Editor, the control
doesn't update until I change something in the aspx and change to
design view. I can't understand why because if you try to update a
DropDownList with the
collection editor, the control updates as soon as you change something
in the Collection Editor.
Anybody what I need to add to this code to make the control to update
when changing something with the collection editor?
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Componen tModel.Design;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.W ebControls;
namespace MyComponents
{
[Serializable]
[ParseChildren(t rue, "Items")]
public class TestControl : WebControl, INamingContaine r
{
private ListItemCollect ion _items;
[DefaultValue(nu ll),
PersistenceMode (PersistenceMod e.InnerDefaultP roperty)]
public ListItemCollect ion Items {
get { return _items; }
}
public override ControlCollecti on Controls {
get {
EnsureChildCont rols();
return base.Controls;
}
}
public TestControl() : base(HtmlTextWr iterTag.Div)
{
_items = new ListItemCollect ion();
}
protected override void CreateChildCont rols()
{
Controls.Add(ne w LiteralControl( "-- TestControl
--<br>"));
for (int i = 0; i < _items.Count; i++){
LiteralControl l = new
LiteralControl( _items[i].Text + "<br>");
Controls.Add(l) ;
}
}
protected override void RenderContents( HtmlTextWriter
writer)
{
foreach(Control ctrl in Controls){
ctrl.RenderCont rol(writer);
}
}
}
}