26

I have the following repeater below and I am trying to find lblA in code behind and it fails. Below the markup are the attempts I have made:

<asp:Repeater ID="rptDetails" runat="server">
    <HeaderTemplate>
        <table>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><strong>A:</strong></td>
            <td><asp:Label ID="lblA" runat="server"></asp:Label>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>
</table>

First I tried,

Label lblA = (Label)rptDetails.FindControl("lblA");

but lblA was null

Then I tried,

Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");

but Items was 0 even though m repeater contains 1 itemtemplate

Xaisoft
  • 45,655
  • 87
  • 279
  • 432

5 Answers5

32

You need to set the attribute OnItemDataBound="myFunction"

And then in your code do the following

void myFunction(object sender, RepeaterItemEventArgs e)
{
   Label lblA = (Label)e.Item.FindControl("lblA");
}

Incidentally you can use this exact same approach for nested repeaters. IE:

<asp:Repeater ID="outerRepeater" runat="server" OnItemDataBound="outerFunction">
<ItemTemplate>
   <asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerFunction">
   <ItemTemplate><asp:Label ID="myLabel" runat="server" /></ItemTemplate>
   </asp:Repeater>
</ItemTemplate>
</asp:Repeater>

And then in your code:

void outerFunction(object sender, RepeaterItemEventArgs e)
{
   Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
   innerRepeater.DataSource = ... // Some data source
   innerRepeater.DataBind();
}
void innerFunction(object sender, RepeaterItemEventArgs e)
{
   Label myLabel = (Label)e.Item.FindControl("myLabel");
}

All too often I see people manually binding items on an inner repeater and they don't realize how difficult they're making things for themselves.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • I'm a little confused. What I am actually doing is when I click on a View details link on 1 page, it takes me to a detailed view on another page which has the repeater and in page_load, I am trying to locate lblA – Xaisoft Jul 29 '09 at 22:11
  • Edited. Hopefully that explains a bit more. – Spencer Ruport Jul 29 '09 at 22:12
  • Ok, what I ended up doing was moving a DataTable variable to the top level of class. Then I created the ItemDataBoundEvent and in there I checked for the lblA in the repeater and if it found it, set it to some text I got back from a row in the DataTable. Am I correct in my understanding of this? Thanks – Xaisoft Jul 29 '09 at 22:26
  • I guess a simple question is, why it was return 0 items when I had 1 item template? – Xaisoft Jul 29 '09 at 22:27
  • I'm not really sure. I never access items directly in a repeater so I'm not sure how they behave. – Spencer Ruport Jul 29 '09 at 23:59
  • I have a some controls inside a repeater. Everytime there is a postback, I have to rebind it before I can access the controls. Is there any way to call the `ItemCreated` function to retrieve the controls without binding it again? – Si8 Mar 01 '16 at 19:20
  • @SiKni8 - I couldn't tell you. It's been so long since I've used ASP.Net Forms I don't remember the magic behaviors which drive this sort of thing. :( – Spencer Ruport Mar 02 '16 at 09:20
  • It is okay. It was just a mess to deal with it. I just manually populated the fields and makes life so much easier. Thank you :) – Si8 Mar 03 '16 at 15:22
8

I just had the same problem.

We are missing the item type while looping in the items. The very first item in the repeater is the header, and header does not have the asp elements we are looking for.

Try this:

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");}
dvdmn
  • 6,456
  • 7
  • 44
  • 52
  • thanks man, I have the same problem and I spend so much time to figure out the problem. SO is great !! – Kamran Jul 26 '15 at 09:56
  • ditto - this really should have been included on MSDNs own example... but what should we expect from that huh?? – Rich Dec 04 '15 at 11:11
2

Code for VB.net

    Protected Sub rptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDetails.ItemDataBound    
      If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
        Dim lblA As Label = CType(e.Item.FindControl("lblA"), Label)
        lblA.Text = "Found it!"
      End If
    End Sub
Kyle B.
  • 5,737
  • 6
  • 39
  • 57
1

Investigate the Repeater.ItemDataBound Event.

Dan Diplo
  • 25,076
  • 4
  • 67
  • 89
0

You should bind first.
for example)

rptDetails.DataSource = dataSet.Tables["Order"];

rptDetails.DataBind();
शेखर
  • 17,412
  • 13
  • 61
  • 117
Peter
  • 1