locked
Repeater add programmatically user control as itemtemplate RRS feed

  • Question

  • User699069345 posted

    Hello, I have the following problem. First Excuse-me for my bad English.

    I'm developping a custom server control, it has to do his job totaly programmatically thanks server control Attributes.

    This control embed a Repeater, I have implemented Itemplate in order to "feed" my Repeater.

    Is it possible to feed programmatically my repeater with user controls ?

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myClass.ascx.cs" Inherits="myClass" %>
     <asp:Label ID="Title" Text='<%# Bind("Title") %>' runat="server"></asp:Label>

    Regards

    Wednesday, July 4, 2012 9:03 AM

Answers

  • User3866881 posted

    If you want to dynammically add controls to the Repeater,I suggest you:

    1)First please add a Panel in the itemTemplate  of your Repeater;

    2)Then in the Repeater_ItemDataBound event,plz handle each RepeaterItem by using the event and find out the Panel inside and dynamically add server-side controls……

    void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    {
    
              // This event is raised for the header, the footer, separators, and items.
    
              // Execute the following logic for Items and Alternating Items.
              if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             {
    
     ((e.Item as RepeaterItem).FindControl("Id of Panel") as Panel).Controls.Clear();
    ((e.Item as RepeaterItem).FindControl("Id of Panel") as Panel).Controls.Add(……); } }


    PS:Considering it that your newly-added controls without "memory" of remembering things because each time when binding it will be disposed and renew one……So please use another Session or ViewState to remember these values if you want to assign to them ……

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:58 PM

All replies

  • User-661350001 posted

    this can be achieved easily by creating dynamic controls

    foreach(var row in rows)

    {

    lbl = new Label();

    lbl.Text = row[0].ToString();

    controsl.Add(lbl);

    .................

    ...............

    etc

    }

    Wednesday, July 4, 2012 11:03 AM
  • User699069345 posted

    Thanks for your reply.

    What about usercontrols in your example ? I know that is possible with server controls. Have I misunderstood something ?

    Thursday, July 5, 2012 12:52 PM
  • User3866881 posted

    If you want to dynammically add controls to the Repeater,I suggest you:

    1)First please add a Panel in the itemTemplate  of your Repeater;

    2)Then in the Repeater_ItemDataBound event,plz handle each RepeaterItem by using the event and find out the Panel inside and dynamically add server-side controls……

    void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
    {
    
              // This event is raised for the header, the footer, separators, and items.
    
              // Execute the following logic for Items and Alternating Items.
              if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             {
    
     ((e.Item as RepeaterItem).FindControl("Id of Panel") as Panel).Controls.Clear();
    ((e.Item as RepeaterItem).FindControl("Id of Panel") as Panel).Controls.Add(……); } }


    PS:Considering it that your newly-added controls without "memory" of remembering things because each time when binding it will be disposed and renew one……So please use another Session or ViewState to remember these values if you want to assign to them ……

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2012 9:58 PM
  • User699069345 posted

    Thanks, I will try.

    Wednesday, July 11, 2012 8:19 AM