Why is my data-bound templated control so slow?

I've been working on a templated control, and I'm astounded at how slow it is. I looked at the execution times and narrowed it down to the actual data binding of a particular item...

TemplateItem item = new TemplateItem();
item.DataItem = simpleContainerClass;
ITemplate template;
if (_postTemplate != null)
{
    template = _postTemplate;
    template.InstantiateIn(item);
    this.Controls.Add(item);
}
item.DataBind();


This block of code runs once for each item in the collection of data items in the control's CreatChildControls() method. The TemplateItem class is just a container class with one property called DataItem. The last line, the DataBind() method, takes nearly .1 seconds for each item. That's obviously not acceptable.

Am I missing something here or is there just something slow about using this? I'm using v2 of .NET, by the way.

5 Comments

  • Hi Jeff-



    When you call DataBind on an item, a lot of stuff happens. Second to control creation, databinding is when the most processing and control population occurs. The databound properties on all the controls inside the item are bound. OnItemDataBound (in some controls) is called, so all logic in the event handler is called. The OnDataBound event on every control inside the item is called. Do you handle or have logic in any of those?



    When DataBinding occurs, if there are Eval or Bind statements on control properties inside the template, the DataBinder class looks for the interface IDataItemContainer on the object being databound to. In this case, it's most likely the SimpleContainerClass. You can speed things up some by ensuring that TemplateItem implements IDataItemContainer.



    Another thing to look for: Does SimpleContainerClass implement ICustomTypeDescriptor? If so, make sure any logic to retrieve the properties is fast.



    Hopefully this will help narrow down why databinding feels slow for you. If not, if you can get your hands on a profiler (like is available in the Team System SKU), this will help locate the culprits.

  • Could it be the timing of the databinding? You are calling databind right after the controls are created.



    If you call the base databind once after calling create child controls, it should recursively call each item's databind.

  • Oddly enough, trying this with a repeater yields even worse results. I don't remember a repeater ever behaving this slow in v1.x.

  • The Repeater control has had very few changes, and virtually none to the code paths you're using. It sounds like reflection on your data item or your databinding statements are taking a long time to execute. You can add some trace statements to different controls' DataBound event handlers to figure out which ones are taking all the time.

  • Yeah... see my next post. I was doing something stupid. It wasn't the binding process at all that was slowing it down.

Comments have been disabled for this content.