0

I have a Repeater control that loads instances of a custom control I have built. This repeater looks like this:

<asp:Repeater ID="myRepeater" runat="server" OnLoad="myRepeater_Load">
  <HeaderTemplate>
    <table border="0" cellpadding="0" cellspacing="0">
  </HeaderTemplate>

  <ItemTemplate>
    <tr><td><my:CustomControl ID="myControl" runat="server" 
      OnLoad="myControl_Load" />
    </td></tr>
  </ItemTemplate>

  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>

I bind to the Repeater through the myRepeater_Load event handler. My custom control is used to render an item within the Repeater. Because of this, I am trying to set properties on the custom control during the myControl_Load event handler. However, I do not know how to access the current item during the myControl_Load event.

Is there a way I can pass along the current item or access the current item during the myControl_Load event? If so, how?

Thank you,

3 Answers 3

3
<asp:Repeater ID="rptrDemo" runat="server" OnItemDataBound="rptrDemo_ItemDataBound">
    <ItemTemplate>
        <demo:Sample runat="server" ID="sampleControl" />
    </ItemTemplate>
</asp:Repeater>


protected void rptrDemo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.DataItem != null)
    {
        SampleControl sampleControl = (SampleControl)e.Item.FindControl("sampleControl");
        // do whatever
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use the repeater's OnItemDatabound event.

4 Comments

But how do I access my custom control from the OnItemDataBound event then?
The event args object will allow you access to your control via e.Item.FindControl() - but you'll have to cast it. Personally I despise OnItemDataBound and avoid it at all costs
@Eric, what would you recommend instead? I would like to know the alternative. Thanks
@goku_da_master The alternative is simply to do whatever data binding is necessary in the ASPX/ASCX markup itself (declaratively)
0

use the OnItemDatabound event of the Repeater

        void r_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CustomControl ctl = (CutonControl)e.Item.FindControl("myControl");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.