Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create HTML table from a list

Tags:

html

c#

asp.net

I am trying to get some values from a List and then create a html table with this data but I can't get it to work properly.

I have:

HtmlTable table = new HtmlTable();
HtmlTableRow row;
HtmlTableCell cell;

foreach(var item in Name)
{
    row = new HtmlTableRow();
    
    foreach(var familyName in item.familyName)
    {
        cell = new HtmlTableCell();
        cell.InnerText = item.familyName.ToString();
        row.Cells.Add(cell);
    }
    
    foreach (var givenName in item.givenName)
    {
        cell = new HtmlTableCell();
        cell.InnerText = item.givenName.ToString();
        row.Cells.Add(cell);
    }
    
    table.Rows.Add(row);
}

this.Controls.Add(table);

When I step through the debugger I can see that row.Cells.Add(cell) contains the family name in the first loop and given name in the second loop but then something seems to be wrong and I can't get the table to show up on the page with this data.

When I check the table.rows.add(row) it says that

base {System.SystemException} = {"'HtmlTableRow' does not support the InnerText property."}

What am I doing wrong here?

like image 420
peter Avatar asked Jan 26 '11 07:01

peter


1 Answers

I've stepped through your code and I can't replicate the error you mention.

It's difficult to say for sure without seeing your data structure Name but a couple of observations:

I. If familyName is a string, your inner foreach will execute once for each character in the string. This may not be what you want as it'll output a surname x number of times where x = surname.length.

This will result in unequal numbers of table cells per row unless all your surnames are the same length.

So I would say get rid of the

foreach(var familyName in item.familyName){...}

loop and just leave the code inside so it'll output surname just once.

II. I'm guessing that item.givenName is an array or collection e.g. List<> of strings? If so you could just use

cell.InnerText = givenName;  

Note that this is will still give you uneven numbers of table cells per row because people have different numbers of forenames ;-)

Having said that you really ought to use the built in controls for doing this kind of thing - the Repeater is probably the way to go.

E.g.

Markup

<asp:Repeater runat="server" id="rptNames" onItemDataBound="rptName_ItemDataBound" >
        <HeaderTemplate>
            <table>
                <tr>
                    <td>Given Name(s)</td>
                    <td>Family Name</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("FamilyName") %></td>
                <td>
                    <asp:Label runat="server" id="lGivenNames" />
                </td>
            </tr>            
        <ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>

CodeBehind

Probably triggered by Page_Load - just bind your repeater to your Name collection:

rptNames.DataSource = Name;

rptNames.DataBind();

To output the GivenNames you use the ItemDataBound event which gets called for each row of the repeater:

protected void rptNames_ItemDataBound(object sender, RepeaterItemEventArgs e){
    //Not interested the Header and Footer rows
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
        Label l = ((Label)e.Item.FindControl("lGivenNames"));

        string[] arrGivenNames = ((FullName)e.Item.DataItem).GivenNames;

        foreach (string n in arrGivenNames){//could use a StringBuilder for a performance boost.
            l.Text += n + "&nbsp;";         //Use a regular space if using it for Winforms
        }
        //For even slicker code, replace the Label in your repeater with another repeater and bind to that. Google `nested repeater` for a how to.
    }
}

HTH.

Full Code

<h2>Doing it by hand - manually building up an HTML Table</h2>

<asp:Panel runat="server" ID="pnl1">
</asp:Panel>

<h2>With a Repeater</h2>

<asp:Repeater runat="server" id="rptNames" onItemDataBound="rptName_ItemDataBound" >
        <HeaderTemplate>
            <table border="1" style="border-color:Red;">
                <tr>
                    <td>Given Name(s)</td>
                    <td>Family Name</td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td><%# Eval("FamilyName") %></td>
                <td>
                    <asp:Label runat="server" id="lGivenNames" />
                </td>
            </tr>     
        </ItemTemplate>       
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace Testbed.WebControls
    {
        internal class FullName{
            public string FamilyName{get;set;}
            public string[] GivenNames{get;set;}
            public FullName(){

            }
            public FullName(string[] _givenNames, string _familyName)
            {
                FamilyName = _familyName;
                GivenNames = _givenNames;
            }
        }
        public partial class HTMLTables : System.Web.UI.Page
        {
            List<FullName> Name;

            protected void Page_Load(object sender, EventArgs e)
            {
                this.Name = new List<FullName>();
                Name.Add(new FullName(new string[]{"Kylie"},"Minogue"));
                Name.Add(new FullName(new string[]{"Angelina", "Kate", "Very-Lovely"}, "Jolie"));
                Name.Add(new FullName(new string[]{"Audrey", "Veronica"},"Hepburn"));

                HtmlTable table = new HtmlTable();
                table.Border = 1;
                HtmlTableRow row;
                HtmlTableCell cell;

                row = new HtmlTableRow();
                cell = new HtmlTableCell();
                cell.InnerText = "Given Name";
                row.Cells.Add(cell);

                cell = new HtmlTableCell();
                cell.InnerText = "Family Name";
                row.Cells.Add(cell);


                foreach (var item in Name)
                {
                    row = new HtmlTableRow();

                    //foreach (var familyName in item.FamilyName){
                        cell = new HtmlTableCell();
                        cell.InnerText = item.FamilyName.ToString();
                        row.Cells.Add(cell);
                    //}
                    foreach (string givenName in item.GivenNames)
                    {
                        cell = new HtmlTableCell();
                        cell.InnerText = givenName.ToString();
                        row.Cells.Add(cell);
                    }

                    table.Rows.Add(row);
                }
                this.pnl1.Controls.Add(table);


                //Or do it with a repeater
                rptNames.DataSource = Name;
                rptNames.DataBind();

            }

            //This gets called everytime a data object gets bound to a repeater row
            protected void rptName_ItemDataBound(object sender, RepeaterItemEventArgs e){
                switch(e.Item.ItemType){
                    case ListItemType.Item:
                    case ListItemType.AlternatingItem:
                        string[] arrGivenNames = ((FullName)e.Item.DataItem).GivenNames;

                        foreach(string n in arrGivenNames){
                            ((Label)e.Item.FindControl("lGivenNames")).Text += n + @"&nbsp;";
                        }
                    break;

                    default:

                    break;
                }
            }
        }
    }
like image 143
indra Avatar answered Sep 30 '22 15:09

indra