Search code examples
c#sharepoint-2010html-table

How to dynamically display loop items in tables?


The following is the design.

<table>
<tr>
<td>Project Title</td>
<td>Download Link</td>
</tr>
<tr>
<td><asp:Label ID="dlLbl" runat="server"></asp:Label></td>
<td><asp:Label ID="dlLink" runat="server"></asp:Label></td>
</tr>
</table>

And the following is the backend codes.

foreach (SPListItem objInnovationListItem in objInnovationList.Items)
        {
            if (Convert.ToString(objInnovationListItem["Innovation Approval Status"])== status)
            {
                countStatus++;

                //Displays name of the document and download link
                dlLbl.Text = objInnovationListItem["Project Title"].ToString();
                dlLink.Text = "<a href='/RIDepartment/Innovation%20Submission/" + objInnovationListItem.File.Name + "'>Download</a><br>";
            }
        }

Hence, my question is, what can I do to allow the tables to dynamically accommodate the document and dl link when there's more than 1 in the loop?

Appreciate some code samples.


Solution

  • With your code style (manual creating html without web-controls) i recommend you to look on ASP.NET MVC side. But i can answer to your question:

    First - you need to use asp:Repeater like this:

    <table>
        <tr>
            <td>Project Title</td>
            <td>Download Link</td>
        </tr>
        <asp:Repeater ID="repLinks" runat="server" 
            onitemdatabound="repLinks_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblProject" runat="server" Text="Label"></asp:Label>
                    </td>
                    <td>
                        <asp:HyperLink ID="hlLink" runat="server">HyperLink</asp:HyperLink>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
    

    second: you need to initialize your collection, that you want to display. For example: you want to display a collection of objInnovationListItem class:

    public class objInnovationListItem
        {
            public string Name { get; set; }
            public string Title { get; set; }
            public override string ToString()
            {
                return Title;
            }
        }
    

    you need do next:

            // list - it's source List<objInnovationListItem>
            var bindList = list.Where(p => objInnovationListItem["Innovation Approval Status"] == status); // filter your collection - replace you foreach and if statement
    
            repLinks.DataSource = bindList; // set to repeater your displayed collection
            repLinks.DataBind(); // bind your collection
    

    and last - you need to indicate in your Repeater ItemTemplate how to display your objInnovationListItem instance - subscribe to event of your Repeater ItemDataBound:

            protected void repLinks_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var item = e.Item.DataItem as objInnovationListItem;
            ((Label) e.Item.FindControl("lblProject")).Text = item.Name;
            ((HyperLink) e.Item.FindControl("hlLink")).NavigateUrl = string.Format("/downloaduri?id={0}", item.Title);
        }
    

    Result will look like that:

    Result