1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Repeater in a custom control

Repeater in a custom control

4 posts, 0 answered
  1. Pierre-Jean
    Pierre-Jean avatar
    8 posts
    Registered:
    29 Jul 2011
    29 Aug 2011
    Link to this post
    Hi,

    I'm experiencing some issues wanting to put a repeater control into a custom control.
    Here is a part of my template :

    <div class="infoGauche">
        <asp:Repeater ID="rpt_descriptifs" runat="server">
            <ItemTemplate>
                <h3><asp:Label ID="lbl_TitreDescriptif" runat="server" /></h3>
                <%#DataBinder.Eval(Container.DataItem, "Descriptif")%>
            </ItemTemplate>
        </asp:Repeater>
    </div>

    Here is my custom control class (I followed the newsrotator guide to create it) :

    01.public class ProductViewer : SimpleView
    02.    {
    03. 
    04.        public int IDProduit { get; set; }
    05.        protected virtual Repeater Rpt_descriptifs
    06.        {
    07.            get
    08.            {
    09.                return base.Container.GetControl<Repeater>("rpt_descriptifs", true);
    10.            }
    11.        }
    12. 
    13.        protected override void InitializeControls(GenericContainer container)
    14.        {
    15.            if (!Page.Request.Params["id"].IsNullOrEmpty())
    16.            {
    17.                int idProd = 0;
    18.                if (!int.TryParse(Page.Request.Params["id"], out idProd))
    19.                {
    20.                    // Manage the errors here
    21.                }
    22.                IDProduit = idProd;
    23. 
    24.            }
    25. 
    26. 
    27.            if (IDProduit > 0)
    28.            {
    29. 
    30.                DataAccessor da = new DataAccessor();
    31.                Produit prod = da.GetProduit(IDProduit);
    32. 
    33.                if (Rpt_descriptifs != null)
    34.                {
    35.                    Rpt_descriptifs.DataSource = prod.Deroulement.ToList();
    36.                    Rpt_descriptifs.ItemDataBound += new RepeaterItemEventHandler(rpt_descriptifs_itemDataBound);
    37.                    Rpt_descriptifs.DataBind();
    38.                }
    39.            }
    40. 
    41.        }
    42. 
    43. 
    44.        protected override string LayoutTemplateName
    45.        {
    46.            get
    47.            {
    48.                return "MDC_ProductViewer.Resources.Views.ProductViewer.ascx";
    49.            }
    50.        }
    51. 
    52.        protected void rpt_descriptifs_itemDataBound(object o, RepeaterItemEventArgs e)
    53.        {
    54.            switch (e.Item.ItemType)
    55.            {
    56.                case ListItemType.Item:
    57.                case ListItemType.AlternatingItem:
    58.                    Deroulement d = e.Item.DataItem as Deroulement;
    59.                    if (d != null)
    60.                    {
    61.                        Label lbl_libelle = e.Item.FindControl("lbl_TitreDescriptif") as Label;
    62.                        if (lbl_libelle != null)
    63.                        {
    64.                            lbl_libelle.Text = "Jour " + d.JourDebut.ToString();
    65.                            if (d.JourFin > 0)
    66.                            {
    67.                                lbl_libelle.Text += " à " + d.JourFin.ToString();
    68.                            }
    69.                            lbl_libelle.Text += " : " + d.Titre;
    70. 
    71.                        }
    72.                    }
    73. 
    74. 
    75. 
    76.                    break;
    77.            }
    78.        }
    79.    }

    Everything goes well till the line 37, where I get an exception :
    "The container must be added to the Page controls collection prior to data binding"

    I tried to make the DataBind at the Page_Load using the Load delegate, but it doesn't seems to be executed (Page.Load += new EventHandler....)

    Could you please tell me why I get this exception and how can I solve it ?

    Thank you.
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    29 Aug 2011
    Link to this post
    I seem to recall encountering this issue a while back. I think that you need to call the databind command later in the sequence. you can do it in the Page_Load event using an override:

    protected override void OnLoad(EventArgs e)
    {
           // databind here
    }

    I believe you can also do it by overriding CreateChildControls()

    Let me know if this doesn't work, hope this was helpful!
  3. Pierre-Jean
    Pierre-Jean avatar
    8 posts
    Registered:
    29 Jul 2011
    30 Aug 2011
    Link to this post
    Hi, and thank you for this answer, but it still doesn't work, I get the same exception.

    And as I saw by placing breakpoints, the OnLoad event is called before the InitializeComponent event, seems a bit weird.

    I'm getting a try with a user control, but I'd like to be able to use my controls in several projects.
    Custom control doesn't seem to like the databinding on a repeater, even if I don't really understand why, because regarding the documentation, the exact same code seems to work correctly for a RadRotator control.

    Thank you for your help.
  4. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    31 Aug 2011
    Link to this post
    Hello Pierre-Jean,

    There was a similar question before:

    http://www.sitefinity.com/devnet/forums/sitefinity-4-x/general-discussions/how-to-databind-asp-net-gridview-in-custom-module.aspx

    As you can see you should override the OnPreRender of the SimpleView and perform the binding there.

    Best wishes,
    Lubomir Velkov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

4 posts, 0 answered