C#在样式表中显示列表
我希望在html表格中显示我的列表,或者我可以设置输出样式的东西 下面是我的代码和一个图片链接,显示了我正在努力实现的目标。 每个订单都应该有自己的表格 我最初使用的是一个转发器,但因为我需要将具有相同ID的项目分组,所以无法使其正确显示 谢谢C#在样式表中显示列表,c#,webforms,html-table,C#,Webforms,Html Table,我希望在html表格中显示我的列表,或者我可以设置输出样式的东西 下面是我的代码和一个图片链接,显示了我正在努力实现的目标。 每个订单都应该有自己的表格 我最初使用的是一个转发器,但因为我需要将具有相同ID的项目分组,所以无法使其正确显示 谢谢 public class myProducts { public int Order { get; set; } public string Date { get; set; } public string Qty
public class myProducts
{
public int Order { get; set; }
public string Date { get; set; }
public string Qty { get; set; }
public string GrandTotal { get; set; }
public string Ship_FirstName { get; set; }
public string Ship_LastName { get; set; }
public string Item { get; set; }
public string Options { get; set; }
}
List<myProducts> products = new List<myProducts>();
myProducts product = new myProducts
{
Order = 1111,
Date = "5/8/2017",
Qty = "2",
GrandTotal = "$50.00",
Ship_FirstName = "John",
Ship_LastName = "Doe",
Item = "Item 4",
Options = "Option1, Option2, Option3, Option4",
};
products.Add(product);
product = new myProducts
{
Order = 1111,
Date = "5/8/2017",
Qty = "2",
GrandTotal = "$50.00",
Ship_FirstName = "John",
Ship_LastName = "Doe",
Item = "Item 4",
Options = "Option1, Option2, Option3, Option4",
};
products.Add(product);
product = new myProducts
{
Order = 34556,
Date = "5/9/2017",
Qty = "2",
GrandTotal = "$200.00",
Ship_FirstName = "John",
Ship_LastName = "Doe",
Item = "Item 4",
Options = "Option1, Option2, Option3, Option4",
};
products.Add(product);
product = new myProducts
{
Order = 143566,
Date = "5/2/2017",
Qty = "2",
GrandTotal = "$100.00",
Ship_FirstName = "John",
Ship_LastName = "Doe",
Item = "Item 4",
Options = "Option1, Option2, Option3, Option4",
};
products.Add(product);
var groups = products.GroupBy(x => new { x.Order, x.GrandTotal, x.Date, x.Ship_FirstName, x.Ship_LastName });
foreach (var group in groups)
{
var line1 = "Order # " + group.Key.Order + " <br />" + "Order Placed: " + group.Key.Date + " <br />" + " Total: " + group.Key.GrandTotal + " <br />" + " Ship To: " + group.Key.Ship_FirstName + " " + group.Key.Ship_LastName;
Line1.Text = Line1.Text + " <br /> " + line1.ToString() ;
foreach (var item in group)
{
var line2 = " -- Item: " +item.Item + " <br /> " + "-- Options: " + item.Options;
Line1.Text = Line1.Text + " <br /> " + line2.ToString();
}
}
公共类myProducts
{
公共整数顺序{get;set;}
公共字符串日期{get;set;}
公共字符串数量{get;set;}
公共字符串GrandTotal{get;set;}
公共字符串Ship_FirstName{get;set;}
公共字符串Ship_LastName{get;set;}
公共字符串项{get;set;}
公共字符串选项{get;set;}
}
列表产品=新列表();
myProducts产品=新的myProducts
{
订单=1111,
日期=“2017年5月8日”,
Qty=“2”,
GrandTotal=“$50.00”,
Ship_FirstName=“John”,
Ship_LastName=“Doe”,
Item=“第4项”,
Options=“Option1、Option2、Option3、Option4”,
};
产品。添加(产品);
产品=我的新产品
{
订单=1111,
日期=“2017年5月8日”,
Qty=“2”,
GrandTotal=“$50.00”,
Ship_FirstName=“John”,
Ship_LastName=“Doe”,
Item=“第4项”,
Options=“Option1、Option2、Option3、Option4”,
};
产品。添加(产品);
产品=我的新产品
{
订单=34556,
日期=“2017年5月9日”,
Qty=“2”,
GrandTotal=“$200.00”,
Ship_FirstName=“John”,
Ship_LastName=“Doe”,
Item=“第4项”,
Options=“Option1、Option2、Option3、Option4”,
};
产品。添加(产品);
产品=我的新产品
{
订单=143566,
日期=“2017年5月2日”,
Qty=“2”,
GrandTotal=“$100.00”,
Ship_FirstName=“John”,
Ship_LastName=“Doe”,
Item=“第4项”,
Options=“Option1、Option2、Option3、Option4”,
};
产品。添加(产品);
var groups=products.GroupBy(x=>new{x.Order,x.GrandTotal,x.Date,x.Ship\u FirstName,x.Ship\u LastName});
foreach(组中的var组)
{
var line1=“Order#”+group.Key.Order+”
“+”下订单:“+group.Key.Date+”
“+”总计:“+group.Key.GrandTotal+”
“+”发货至:“+group.Key.Ship_FirstName+”+group.Key.Ship_LastName;
Line1.Text=Line1.Text+“
”+Line1.ToString();
foreach(组中的var项目)
{
var line2=“--Item:“+Item.Item+”
“+”--Options:“+Item.Options;
Line1.Text=Line1.Text+“
”+line2.ToString();
}
}
您可以在此处使用嵌套中继器,并将分组数据绑定到用作标题的外部中继器,将每组中的项目列表绑定到内部中继器,如下所示: .aspx:
<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
<table class="table-styling">
<thead>
<th>Order Placed:
<br />
<%# Eval("Date") %></th>
<th>Total:
<br />
<%# Eval("GrandTotal") %></th>
<th>Ship To:
<br />
<%# Eval("Ship_FirstName") %> <%# Eval("Ship_LastName") %></th>
<th>Order # <%# Eval("Order") %></th>
</thead>
<tbody>
<asp:Repeater runat="server" DataSource='<%# Eval("OrderItems") %>'> <%--binding item list to the inner repeater--%>
<ItemTemplate>
<tr>
<td colspan="4">Qty: <%# Eval("Qty") %></td>
</tr>
<tr>
<td colspan="4">Item: <%# Eval("Item") %></td>
</tr>
<tr>
<td colspan="4">Options: <%# Eval("Options") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</ItemTemplate>
</asp:Repeater>
然后可以将样式添加到中继器
或者,您可以为foreach循环中的表生成HTML代码,包括用于自定义样式的必要CSS类,例如:
foreach (var group in groups)
{
var html = new StringBuilder();
html.Append("<table class="table-styling">");
html.Append($"<thead><th>Order # {group.Key.Order}</th><th>Order Placed: <br />{group.Key.Date}</th><th>Total: <br />{group.Key.GrandTotal}</th><th> Ship To: <br />{group.Key.Ship_FirstName} {group.Key.Ship_LastName}</th></thead>";
html.Append("<tbody>");
foreach (var item in group)
{
html.Append($"<tr><td colspan=\"4\">Qty: {}</td></tr>");
html.Append($"<tr><td colspan=\"4\">Item: {item.Item}</td></tr>");
html.Append($"<tr><td colspan=\"4\">Options: {item.Options}</td></tr>");
}
html.Append("</tbody></table>")
Line1.Text = Line1.Text + " <br /> " + html.ToString();
}
不过,我会选择第一个选项——将HTML生成为文本很麻烦:)Platform?Winform/ASP web Form/MVC?@MAdeelKhalid抱歉,ASP.net web Form谢谢您的帮助!对于中继器,每个分组都有数量、项目和选项。您能否提供如何将其添加到代码中?有些订单会有多个产品。这就是为什么我们有foreach循环@达什维达
foreach (var group in groups)
{
var html = new StringBuilder();
html.Append("<table class="table-styling">");
html.Append($"<thead><th>Order # {group.Key.Order}</th><th>Order Placed: <br />{group.Key.Date}</th><th>Total: <br />{group.Key.GrandTotal}</th><th> Ship To: <br />{group.Key.Ship_FirstName} {group.Key.Ship_LastName}</th></thead>";
html.Append("<tbody>");
foreach (var item in group)
{
html.Append($"<tr><td colspan=\"4\">Qty: {}</td></tr>");
html.Append($"<tr><td colspan=\"4\">Item: {item.Item}</td></tr>");
html.Append($"<tr><td colspan=\"4\">Options: {item.Options}</td></tr>");
}
html.Append("</tbody></table>")
Line1.Text = Line1.Text + " <br /> " + html.ToString();
}
.table-styling {
// your style
}
.table-styling th {
// your style
}
.table-styling td {
// your style
}