gpt4 book ai didi

c# - 从列表创建 HTML 表格

转载 作者:太空狗 更新时间:2023-10-29 13:13:05 24 4
gpt4 key购买 nike

我试图从列表中获取一些值,然后用这些数据创建一个 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);

当我单步执行调试器时,我可以看到 row.Cells.Add(cell) 在第一个循环中包含姓氏,在第二个循环中包含给定名称,但似乎出了点问题,我无法获取表格显示在包含此数据的页面上。

当我检查 table.rows.add(row) 时,它说

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

我在这里做错了什么?

最佳答案

我已经逐步检查了您的代码,但无法重现您提到的错误。

如果没有看到您的数据结构 Name 很难确定,但有几点观察:

我。如果 familyName 是一个字符串,您的内部 foreach 将为字符串中的每个字符执行一次。这可能不是您想要的,因为它会输出姓氏x 次,其中x = 姓氏.长度。

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

所以我会说摆脱

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

循环并将代码留在里面,这样它只会输出姓氏一次。

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

cell.InnerText = givenName;  

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

话虽如此,您确实应该使用内置控件来执行此类操作 - Repeater 可能是最佳选择。

例如

标记

<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 触发 - 只需将您的中继器绑定(bind)到您的 Name 集合:

rptNames.DataSource = Name;

rptNames.DataBind();

要输出 GivenName,您可以使用 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;
}
}
}
}

关于c# - 从列表创建 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com