instantiating a template control

A

Andy Fish

Hi,

Say I had a repeater-like situation but were I wanted to have every 3rd item
look different instead of every alternating one. I could write my own custom
control and in the ASPX file something like this:

<myControl>
<mainItemTemplate>...</>
<secondItemTemplate>...</>
<thirdItemTemplate>...</>
</myControl>

The question is, in the data binding logic, how can I instantate the
template control and turn it into a real control? do I have to use
MemberwiseClone()

This is not an actual example - it's just there to illustrate the question.
In real life I want to be able to select between a number of templates based
on attribute values within the databound object

TIA

Andy
 
K

Karl Seguin

Andy:
You can dynamically load templates via the Page.LoadTemplate:
http://www.aspdotnetheaven.com/Code/Jan2003/DynamicTemplate.asp

Although, from what I understand, you want to load content differently
within a template based on some value. What I would do is put a placeholder
in the ItemTemplate, then during the onItemDataBound control, I would load a
user control and place it inside the template, something like:

protected void itemCreatedRepeater_ItemCreatedobject source,
RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item){
PlaceHolder plc = (PlaceHolder)e.Item.FindControl("ph");
if (plc != null){
DataRowView dv = (DataRowView)e.Item.DataItem;
Control c = null;
if (dv["IsSomething"].ToString() == "blah"){
c = Page.LoadControl("case1.ascx")
}else{
c = Page.LoadControl("case2.ascx")
}
if (c != null){
plc.Controls.Add(c);
}
}
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
A

Andy Fish

Thanks for the reply Karl,

I did consider this approach but I was concerned about the overhead of doing
a LoadControl() for each item in the repeater. In my application there could
be hundreds of items in the list (and each item will be relatively small) so
it seems a very inefficient way of doing it.

Andy

Karl Seguin said:
Andy:
You can dynamically load templates via the Page.LoadTemplate:
http://www.aspdotnetheaven.com/Code/Jan2003/DynamicTemplate.asp

Although, from what I understand, you want to load content differently
within a template based on some value. What I would do is put a
placeholder
in the ItemTemplate, then during the onItemDataBound control, I would load
a
user control and place it inside the template, something like:

protected void itemCreatedRepeater_ItemCreatedobject source,
RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType ==
ListItemType.Item){
PlaceHolder plc = (PlaceHolder)e.Item.FindControl("ph");
if (plc != null){
DataRowView dv = (DataRowView)e.Item.DataItem;
Control c = null;
if (dv["IsSomething"].ToString() == "blah"){
c = Page.LoadControl("case1.ascx")
}else{
c = Page.LoadControl("case2.ascx")
}
if (c != null){
plc.Controls.Add(c);
}
}
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Andy Fish said:
Hi,

Say I had a repeater-like situation but were I wanted to have every 3rd item
look different instead of every alternating one. I could write my own custom
control and in the ASPX file something like this:

<myControl>
<mainItemTemplate>...</>
<secondItemTemplate>...</>
<thirdItemTemplate>...</>
</myControl>

The question is, in the data binding logic, how can I instantate the
template control and turn it into a real control? do I have to use
MemberwiseClone()

This is not an actual example - it's just there to illustrate the question.
In real life I want to be able to select between a number of templates based
on attribute values within the databound object

TIA

Andy
 
A

Andy Fish

OK, I think I found the solution, it's called a templated control. MSDN has
examples for templated control and also templated user control.

There's a lot of new (to me) terminology in there, but I'm pretty sure it's
what I was looking for

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top