문제

아래에 다음 리피터가 있고 코드에서 LBLA를 찾으려고 노력하고 있으며 실패합니다. 마크 업 아래에는 내가 한 시도가 있습니다.

<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>

먼저 나는 시도했다.

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

그러나 LBLA는 Null이었다

그런 다음 시도했습니다.

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

그러나 m 리피터에 1 개의 itemtemplate이 포함되어 있어도 항목은 0이었다.

도움이 되었습니까?

해결책

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.

다른 팁

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");}

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

Investigate the Repeater.ItemDataBound Event.

You should bind first.
for example)

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

rptDetails.DataBind();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top