views:

3305

answers:

4

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

+4  A: 

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
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
Edited. Hopefully that explains a bit more.
Spencer Ruport
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
I guess a simple question is, why it was return 0 items when I had 1 item template?
Xaisoft
I'm not really sure. I never access items directly in a repeater so I'm not sure how they behave.
Spencer Ruport
+1  A: 

Investigate the Repeater.ItemDataBound Event.

Dan Diplo
A: 

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.
A: 

You should bind first.

for example)

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

rptDetails.DataBind();

Peter