gpt4 book ai didi

c# - 将通用列表绑定(bind)到转发器 - ASP.NET

转载 作者:IT王子 更新时间:2023-10-29 04:03:02 25 4
gpt4 key购买 nike

我正在尝试绑定(bind) List<AreaField>到中继器。我已使用 ToArray() 将列表转换为数组方法,现在有一个 AreaField[] 的数组

这是我的类层次结构

public class AreaFields
{
public List<Fields> Fields { set; get; }
}

public class Fields
{
public string Name { set; get; }
public string Value {set; get; }
}

在aspx中,我想给它绑定(bind)一个转发器(类似这样的东西)

DataBinder.Eval(Container.DataItem, "MyAreaFieldName1")

MyAreaFieldName1 是 AreaFieldItem 类中 Name 属性的值。

最佳答案

出乎意料的简单...

代码隐藏:

// Here's your object that you'll create a list of
private class Products
{
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductPrice { get; set; }
}

// Here you pass in the List of Products
private void BindItemsInCart(List<Products> ListOfSelectedProducts)
{
// The the LIST as the DataSource
this.rptItemsInCart.DataSource = ListOfSelectedProducts;

// Then bind the repeater
// The public properties become the columns of your repeater
this.rptItemsInCart.DataBind();
}

ASPX代码:

<asp:Repeater ID="rptItemsInCart" runat="server">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Product Name</th>
<th>Product Description</th>
<th>Product Price</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("ProductDescription")%></td>
<td><%# Eval("ProductPrice")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>

希望对您有所帮助!

关于c# - 将通用列表绑定(bind)到转发器 - ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/674204/

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