[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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
0
paclaiborne
Asked:
paclaiborne
  • 5
  • 3
1 Solution
 
tovvenkiCommented:
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
0
 
paclaiborneAuthor Commented:
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

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

Thanks and regards,
Venki
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
tovvenkiCommented:
if that's the case in the data repeater's Itemdatabound event you can use datarepeater.FindControl to get the datalist and set its datasource. This will bind the datalist.
As i don't have VS right now I could not test it.

I hope that this helps you.

Thanks and regards,
Venki

0
 
paclaiborneAuthor Commented:
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).
0
 
paclaiborneAuthor Commented:
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

0
 
paclaiborneAuthor Commented:
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?
0
 
paclaiborneAuthor Commented:
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

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now