Dot*_*ude 35 c# asp.net repeater
我试图绑定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; }
}
Run Code Online (Sandbox Code Playgroud)
在aspx中,我想绑定一个转发器(类似这样)
DataBinder.Eval(Container.DataItem, "MyAreaFieldName1")
Run Code Online (Sandbox Code Playgroud)
MyAreaFieldName1是AreaFieldItem类中Name属性的值.
Sol*_*urn 65
这简直太简单了......
代码背后:
// 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();
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!
ben*_*wey 23
您可能想要创建一个subRepeater.
<asp:Repeater ID="SubRepeater" runat="server" DataSource='<%# Eval("Fields") %>'>
<ItemTemplate>
<span><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)
你也可以投射你的领域
<%# ((ArrayFields)Container.DataItem).Fields[0].Name %>
Run Code Online (Sandbox Code Playgroud)
最后,您可以执行一些CSV函数并使用函数写出您的字段
<%# GetAsCsv(((ArrayFields)Container.DataItem).Fields) %>
public string GetAsCsv(IEnumerable<Fields> fields)
{
var builder = new StringBuilder();
foreach(var f in fields)
{
builder.Append(f);
builder.Append(",");
}
builder.Remove(builder.Length - 1);
return builder.ToString();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
代码背后:
public class Friends
{
public string ID { get; set; }
public string Name { get; set; }
public string Image { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
List <Friends> friendsList = new List<Friends>();
foreach (var friend in friendz)
{
friendsList.Add(
new Friends { ID = friend.id, Name = friend.name }
);
}
this.rptFriends.DataSource = friendsList;
this.rptFriends.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
.aspx
<asp:Repeater ID="rptFriends" runat="server">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID") %></td>
<td><%# Eval("Name") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Run Code Online (Sandbox Code Playgroud)