c# – 从列表中创建HTML表

前端之家收集整理的这篇文章主要介绍了c# – 从列表中创建HTML表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从列表中获取一些值,然后创建一个带有这个数据的html表,但是我无法使它正常工作.

我有:

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

当我浏览调试器时,我可以看到该行.Cells.Add(单元格)包含第一个循环中的系列名称,并在第二个循环中包含名称,但是似乎有些错误,我无法得到表格显示页面上与此数据.

当我检查table.rows.add(row)时,它表示“base {System.SystemException} = {”’HtmlTableRow’不支持InnerText属性.“}”

我在这里做错了什么?

解决方法

我已经通过你的代码,我不能复制你提到的错误.

很难说,确实没有看到你的数据结构名称,但一些观察:

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

这将导致每行不同数目的表格单元格,除非您的所有姓氏的长度相同.

所以我会说摆脱的

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

循环,只需将代码放在里面,这样就可以输出一次姓氏.

II.我猜,item.givenName是一个数组或集合,例如列表与LT;>的字符串?如果是这样,你可以使用

cell.InnerText = givenName;

请注意,这仍然会给您每行不平的数字表格单元格,因为人们有不同的名字数量;-)

说你真的应该使用内置的控件来做这种事情 – 中继器可能是要走的路.

例如.

标记

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

代码隐藏

可能由Page_Load触发 – 只需将您的中继器绑定到您的名称集合:

rptNames.DataSource = Name;

rptNames.DataBind();

输出GivenNames,您可以使用为中继器的每一行调用的ItemDataBound事件:

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.

完整代码

<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;
                }
            }
        }
    }
原文链接:https://www.f2er.com/csharp/94115.html

猜你在找的C#相关文章