LA.NET [EN]

Apr 10

Back from vacations: why doesn''t the CompositeControlDesigner works properly?

Posted in ASP.NET      Comments Off on Back from vacations: why doesn''t the CompositeControlDesigner works properly?

Or, what am I doing wrong? (again, I”m hoping that it”s my fault)

After ending my vacations (which, unfortunately, ended yesterday), I”ve already started doing some experiences. This time, I thought it was time to port some of my old ASP.NET 1.1 controls to 2.0. As you”ve probably guessed, I”m already facing weird problems. Scenario: I”m developing a simple composite control and I”m unable to set the properties of the control on the designer. Here”s  a small dummy control that illustrates the problem:

public class Demo2:CompositeControl
{
 private TextBox _text;
 public string Text
 {
  get
  {
       EnsureChildControls();
       return _text.Text;
  }
  set
  {
      EnsureChildControls();
      _text.Text = value;
  }
 }

 protected override void CreateChildControls()
 {
    this.Controls.Clear();
    _text = new TextBox();
    _text.ID = “innerText”;
    this.Controls.Add( _text );

   ChildControlsCreated = true;
 }
}

Now, If I”m not mistaken, I”m using the correct base class for my new control. CompositeControl implements the INamingContainer interface and should give me garantees about control initialization (override of the Controls property, which calls EnsureChildControls) and better integration with the designer (implementation of the ICompositeControlDesignerAccessor interface). Well, it seems like the designer integration part is not working properly. If I set the Text property on the designer properties window,the property is persisted on the aspx file but the value is removed from the property window and the textbox isn”t rendered with the property”s value on the designer surface.

The problem is related to the way the CompositeControlDesigner overrides the GetDesignHtml method: it insists in recreating all the controls (ie,it sets the ChildControlsCreated flag to false and calls the EnsureChildControls method, which recreates the control hierarchy).  There are some solutions to solve this problem (you can use the ViewState to persist the values of the properties, you can change the designer associated with the CompositeControl class or you can even do something like this)

Now, since I”m a firm believer that this behavior exists for some reason (and I do believe that MS guys do know what they”re doing), I wonder if anyone can explain me why the CompositeControlDesigner works like this…