Adding custom template to repeater

Discussion in 'ASP .Net Building Controls' started by HmFireBall, Jan 5, 2006.

  1. HmFireBall

    HmFireBall Guest

    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
    HmFireBall, Jan 5, 2006
    #1
    1. Advertising

  2. HmFireBall

    chris martin Guest

    > 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; }
    }
    }
    chris martin, Jan 5, 2006
    #2
    1. Advertising

  3. HmFireBall

    HmFireBall Guest

    chris martin a écrit :
    >> 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; }
    > }
    > }
    > }
    >
    >


    Ok I'll give it a try.
    Thank you very much!
    HmFireBall, Jan 12, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmF5YnVmZmV0?=
    Replies:
    9
    Views:
    848
    Phillip Williams
    Feb 21, 2006
  2. Chris Theis
    Replies:
    2
    Views:
    467
    Chris Theis
    Jul 24, 2003
  3. tom_usenet
    Replies:
    0
    Views:
    533
    tom_usenet
    Jul 24, 2003
  4. Replies:
    1
    Views:
    2,102
    Gianni Mariani
    Jun 8, 2007
  5. dotNet

    Custom template in Repeater

    dotNet, Feb 8, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    225
    IgorW
    Feb 8, 2005
Loading...

Share This Page