Link to home
Start Free TrialLog in
Avatar of paclaiborne
paclaiborne

asked on

How do I build a datalist inside a repeater?

I have a series of tables that I need to display. I want to put the table name as a label that can be linked to from the top of the page. An image with the basic idea is attached.

I thought I could bind each table to a datalist and display each datalist inside a repeater control but I am having a lot of difficulty figuring this one out.

I have tried constructing a template but I don't know how to get my table into the template. (The template is included below.)

Any advice would be extremely welcome.
public class ClientListTemplate : System.Web.UI.ITemplate
    {
        #region Variables

        ListItemType _templateType;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ClientListTemplate"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public ClientListTemplate(ListItemType type)
        {
            _templateType = type;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Instantiates the template
        /// </summary>
        /// <param name="container">The container.</param>
        public void InstantiateIn(Control container)
        {
            PlaceHolder ph = new PlaceHolder();
            HyperLink hl = new HyperLink();
            hl.ID = "hlClient";
            ph.Controls.Add(hl);

            container.Controls.Add(ph);
        }

        /// <summary>
        /// Handles the DataBinding event of the Item control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        static void Item_DataBinding(object sender, EventArgs e)
        {
            PlaceHolder ph = (PlaceHolder)sender;
            DataListItem dli = (DataListItem)ph.NamingContainer;
            string linkText = (string)DataBinder.Eval(dli.DataItem, "ClientName");
            string link = "clientPage.aspx?clientID=" + (string)DataBinder.Eval(dli.DataItem, "ClientID");
            ((HyperLink)ph.FindControl("hlClient")).Text = linkText;
            ((HyperLink)ph.FindControl("hlClient")).NavigateUrl = link;
        }

        #endregion
    }
</script>

Open in new window

12-16-2009-4-08-23-PM.png
Avatar of tovvenki
tovvenki

hey,
What do you mean by "I don't know how to get my table into the template. "
can you give us more info.

You can also refer to this article on implementing the ITemplate interface
http://msdn.microsoft.com/en-us/magazine/cc163780.aspx

Thanks and regards,
Venki
Avatar of paclaiborne

ASKER

Yeah, sorry the remark about getting the table into the template is not very clear.

The datalist is inside the repeater itemtemplate and I do not know how to access the datatable i want to use as the datasource. When I try to set it in the code behind visual studio cannot find the datalist (I guess because it's not created until runtime?). So how do I set the datasource or the template I want to use on a control that is already in another controls itemtemplate?

I hope that's a little clearer. Thanks for your time.
<asp:Repeater ID="rptClients" runat="server">
                                        <ItemTemplate>
                                            <asp:DataList ID="dlClients" runat="server">                                                
                                            </asp:DataList>
                                        </ItemTemplate>
                                    </asp:Repeater>

Open in new window

so you want to set datatable to the datalist that is within the repeater control.
Am I correct

Thanks and regards,
Venki
ASKER CERTIFIED SOLUTION
Avatar of tovvenki
tovvenki

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, I want to set the datatable to datalist that is within the repeater.

I think I have already tried the repeater.FindControl to set the datalist datasource but I'll try again (I've tried so many things I can't remember them all and I'm sure I probably screwed up a couple because of that confusion).
The repeater.FindControl method does not find the datalist. When I run the page the datalist "dl" is null.
DataList dl = (DataList)rptList1.FindControl("dlClients");
dl.DataSource = dt;

Open in new window

I think i may have confused the issue here. The repeater control is bound to a list of tables
List<datatable> tableList
so
 rptList1.datasource = tablelist;
 rptList1.databind();

I have to stop through the tablelist to get each table to bind the datalist and I sure this is my problem. I have been trying to do this in Page_Load but that is pretty obviously not where I should be doing it.

Is there something like the rowdatabound event in a gridview that I can use in the repeater for this?
Okay, apparently I don't think things through sufficiently. The repeater has an event OnItemDataBound, much like the onrowdatabound event in a gridview. To set the datatable as the datasource for the datlist simply implement this event and then do something like the code below.

Thanks for the help Venki.
protected void rptList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            DataList dl = (DataList)e.Item.FindControl("dlClients");
            dl.DataSource = ClientTableList[e.Item.ItemIndex];
            dl.DataBind();
        }
    }

Open in new window