Composite Control with DataList setting Template / Property

  • Thread starter Thread starter berny.zamora
  • Start date Start date
B

berny.zamora

Hello everyone,

I have a composite control (lets call it the parent) that contains a
datalist. The datalist has an ItemTemplate that contains another
composite control (lets call it the child). I am trying to create a
property at the parent control that sets a property in the child
instances.

So for example lets say I am creating a control that displays a list
of hotel reservations for a group of people.

Hierarchy:

HotelControl (CompositeControl)
--DataList
----ItemTemplate
------ReservationInfoControl (CompositeControl)

I want a property for the HotelControl that sets a property for the
ReservationInfoControl instances.

For example.

myHotelControl.ShowDatesAsDropdown = true;

This property controls whether the child instances will show dates as
a dropdown, otherwise it should show as a "calendar". I set this
property on the page's PageLoad event.

The Problem
------------------
I have tried creating a constructor for the ItemTemplate that takes in
the property from the parent control "OnInit". The problem is OnInit
fires before my property is set. I have also tried creating a property
in HotelControl that takes in the ItemTemplate, but have the same
timing issue.

If I set my property on the page's PreInit, it works, but that is not
the standard behavior of other asp.net controls.

So my question is, are there any examples with what I am trying to do?
It sounds like a fairly standard problem, but I just can't find which
Event to set my properties in. My second thought was to Recreate the
child controls when the property is set but that seemed hackish.

Any ideas / examples?

Thx, BZ
 
I found another way of doing this...

I know reset the datalist's item template on property set in the
parent control. For example:

set {
EnsureChildControls();

ViewState["ShowDatesAsDropdown"] = value;

//update item template
MyItemTemplate itemTemplate = new MyItemTemplate(value);
mDataList.ItemTemplate = itemTemplate;
}

That seems to work and can now set the property in PageLoad.

Is this the standard way to handle this scenario?

Thx.
 
Back
Top