Adding custom template to repeater

H

HmFireBall

Hi,

I want to display a dynamic menu using a custom control extending Repeater.
I need a SelectedItemTemplate a bit like in DataList so that I can
highlite the selected menu item.
Basically, it would look like :

..aspx :

<myns:Repeater runat="server" id="MyRepeater">
<ItemTemplate>
<!-- render a selectable menu item here -->
</ItemTemplate>
<SelectedItemTemplate>
<!-- render the selected menu item here -->
</SelectedItemTemplate>
</myns:Repeater>

..aspx.vb :

MyRepeater.DataSource = MyTable
MyRepeater.DataBind()
MyRepeater.SelectedIndex = MyRowIndex

I can't find any tutorial that explain how to add such a custom template
to Repeater. I think that most of the work would be done if I could
define SelectedItemTemplate extending ItemTemplate, and add a switch
somewhere to tell the control to render the selected RepeaterItem using
SelectedItemTemplate and not ItemTemplate. But I need some help there :)

Thank you if you can help me.

Henri
 
C

chris martin

Hi,
I want to display a dynamic menu using a custom control extending
Repeater.
I need a SelectedItemTemplate a bit like in DataList so that I can
highlite the selected menu item.
Basically, it would look like :
.aspx :

<myns:Repeater runat="server" id="MyRepeater">
<ItemTemplate>
<!-- render a selectable menu item here -->
</ItemTemplate>
<SelectedItemTemplate>
<!-- render the selected menu item here -->
</SelectedItemTemplate>
</myns:Repeater>
.aspx.vb :

MyRepeater.DataSource = MyTable
MyRepeater.DataBind()
MyRepeater.SelectedIndex = MyRowIndex
I can't find any tutorial that explain how to add such a custom
template to Repeater. I think that most of the work would be done if I
could define SelectedItemTemplate extending ItemTemplate, and add a
switch somewhere to tell the control to render the selected
RepeaterItem using SelectedItemTemplate and not ItemTemplate. But I
need some help there :)

Thank you if you can help me.

Henri

You don't really extend ItemTemplate. It's the ITemplate interface that you're
looking for.

You will need to, as you know, extend Repeater and add your template there.
The following class is very much UN-tested but, should set you well on your
way.

using System.Web.UI;
using System.Web.UI.WebControls;

namespace lib.webcontrols
{
/// <summary>
/// Summary description for TEMP.
/// </summary>
public class MyRepeater : Repeater
{
private ITemplate selectedItemTemplate;

public MyRepeater()
{}

protected override void InitializeItem(RepeaterItem item)
{
base.InitializeItem(item);

if (item.ItemType == ListItemType.SelectedItem)
{
if (selectedItemTemplate != null)
{
selectedItemTemplate.InstantiateIn(item);
}
}
}

public ITemplate SeletedItemTemplate
{
get { return selectedItemTemplate; }
set { selectedItemTemplate = value; }
}
}
 
H

HmFireBall

chris martin a écrit :
You don't really extend ItemTemplate. It's the ITemplate interface that
you're looking for.

You will need to, as you know, extend Repeater and add your template
there. The following class is very much UN-tested but, should set you
well on your way.

using System.Web.UI;
using System.Web.UI.WebControls;

namespace lib.webcontrols
{
/// <summary>
/// Summary description for TEMP.
/// </summary>
public class MyRepeater : Repeater
{
private ITemplate selectedItemTemplate;

public MyRepeater()
{}

protected override void InitializeItem(RepeaterItem item)
{
base.InitializeItem(item);

if (item.ItemType == ListItemType.SelectedItem)
{
if (selectedItemTemplate != null)
{
selectedItemTemplate.InstantiateIn(item);
}
}
}

public ITemplate SeletedItemTemplate
{
get { return selectedItemTemplate; }
set { selectedItemTemplate = value; }
}
}
}

Ok I'll give it a try.
Thank you very much!
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top