从列表中创建HTML表 [英] Create HTML table from a list

查看:281
本文介绍了从列表中创建HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表中得到一些值,然后创建一个HTML表格这个数据,但我不能让它正常工作。

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.

我有:

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);

当我通过调试步骤,我可以看到row.Cells.Add(单元)包含在第一循环中的系列名称和在第二循环中给定的名字,但随后有些事情似乎是错误的,我不能让表中显示了这个数据在页面上。

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.

当我检查table.rows.add(行)它说, {基地System.SystemException} = {'HtmlTableRow不支持InnerText属性。}

When I check the table.rows.add(row) it says that "base {System.SystemException} = {"'HtmlTableRow' does not support the InnerText property."}"

我在做什么错在这里?

推荐答案

我已经通过你的code踩,我不能复制你提到的错误。

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:

我。如果 familyName 是一个字符串,你内心的的foreach 将字符串中的每个字符执行一次。这的不可以的是你想要的,因为它会输出次数的姓x个,其中x = surname.length。

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.

所以,我要说摆脱

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

环和刚刚离开的code里面所以它会输出姓一次。

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

二。我的猜测的是 item.givenName 是一个数组或集合例如清单<字符串>?如果是这样,你可以只用

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;  

请注意,这是会的还是的给你每行的表细胞的数量不平衡的,因为人有forenames不同的号码; - )

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.

例如

标记

<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

大概到的Page_Load 触发 - 只要你的中继绑定到你的名称系列:

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

rptNames.DataSource = Name;

rptNames.DataBind();

要输出给定名称是你用它获取呼吁中继器中的每一行ItemDataBound事件:

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.
    }
}

心连心。

展开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;
                }
            }
        }
    }

这篇关于从列表中创建HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆