Search code examples
c#.netwebformsuser-controlsrepeater

Why can't I access to a UserControl custom property inside a Repeater?


I'm doing a Data Binding for a Repeater:

<asp:Repeater ID="rptAlumni" runat="server" OnItemDataBound="rptAlumni_DataBinding">
    <ItemTemplate>
        <uc1:AlumnoBox runat="server" ID="AlumnoBox" />
    </ItemTemplate>
</asp:Repeater>

protected void rptAlumni_DataBinding(object sender, RepeaterItemEventArgs e)
{
    var item = (UserControl)e.Item.FindControl("AlumnoBox");
}

but I can't do item.MyCustomID for example, I can't see that property. Instead, if I move the UserControl outside the Repeater and I do AlumnoBox.MyCustomID inside the Page Load it works as well.

Where am I wrong?


Solution

  • Your UserControl inherits from UserControl but you have added addtional properties like MyCustomID. Of course you can't access those properties if you cast to UserControl since the base class does not know them. Instead cast to the right type AlumnoBox:

    var item = (context_box_AlumnoBox) e.Item.FindControl("AlumnoBox"); 
    int id = item.MyCustomID;