简体   繁体   中英

Error accessing a nested repeater in parent ItemDataBound event

Error:

'Repeater' does not contain a definition for 'DataSource' and no extension method 'DataSource' accepting a first argument of type 'Repeater' could be found (are you missing a using directive or an assembly reference?)

Code:

 protected void rptIndicator_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Repeater r2 = (Repeater)e.Item.FindControl("rptActivity");
        r2.DataSource = dt; //Error on this line.
        r2.DataBind();
    }

Markup:

<asp:Repeater ID="rptIndicator" runat="server" OnItemDataBound="rptIndicator_ItemDataBound">
        <ItemTemplate>
            <asp:Repeater ID="rptActivity" runat="server">
                <ItemTemplate>
                    <asp:Repeater ID="rptActivityData" runat="server"></asp:Repeater>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

Can you please help. Why .DataSource is giving me error here.

Thanks.

Do you have some other reference that includes a Repeater class in its namespace?

Perhaps try casting it to System.Web.UI.WebControls.Repeater .

First, you need to check the ItemType or else you'll be back wondering why you're getting a runtime error.

switch (e.Item.ItemType) {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
         Repeater r2 = (Repeater)e.Item.FindControl("rptActivity");
         r2.DataSource = dt; //Error on this line.
         r2.DataBind();
 }

I'm guessing this is a typo, but your markup lists the repeater as " rptActivityData " but in your code you are looking for FindControl("rptActivity")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM