Hi,
You want to provde templates in code? In that case:
1. Create a method to build the child control hierarchy (templates from
datasource) and so that the method takes a parameter to indicate that is
data source to be used or not (say boolean=true for using datasource, false
not using). Method can be named CreateControlHierarchy for example. If True
is given in the argument, method creates control hierarchy (templates) from
the datasource (DataSource proeprty) and stores enough information to
ViewState or equivalent about the templates (count etc and type) so that
they can be recreated on non-databind request (just recreating them is
enough, other stuff is restored by ViewState) that is, when
CreateControlHierarchy is called with argument False.
2. Override Control's DataBind method and do following steps
-Call base class's OnDataBinding method
- call Controls.Clear()
- call ClearChildViewState()
-call TrackViewState()
-call CreateControlHierarchy(true) (to indicate that child controls are
built from datasource)
-Set "ChildControlsCreated=True"
3. Override CreateChildControls method:
-call Controls.Clear()
-call CreateControlHierarchy(false)
Idea here is that you set the DataSource for the control and call DataBind
when it is bound. As a result, control builds its hierarchy based on
datasource(templates and values into child controls of template from
datasource). On other requests, when DataBind is not called (control is not
databound) only CreateChildControls is called and just control hierarchy
(that is templates of correct types andsame count, not anything else) is
rebuilt. The actual data/values are restored by ViewState.