绑定泛型列表到中继器 - ASP.NET [英] Binding a generic list to a repeater - ASP.NET
本文介绍了绑定泛型列表到中继器 - ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图绑定一个列表与LT; AreaField>
到中继器。我已经使用 ToArray的()
方法转换列表到一个数组,现在有一个数组 AreaField []
I am trying to bind a List<AreaField>
to a repeater. I have converted the list into an array by using the ToArray()
method and now have a array of AreaField[]
下面是我的类层次
public class AreaFields
{
public List<Fields> Fields { set; get; }
}
public class Fields
{
public string Name { set; get; }
public string Value {set; get; }
}
在ASPX,我想绑定一个中继器(像这样)
In the aspx, I would like to bind it a repeater (something like this)
DataBinder.Eval(Container.DataItem, "MyAreaFieldName1")
该MyAreaFieldName1在AreaFieldItem类的名称属性的值。
The MyAreaFieldName1 is the value of the Name property in the AreaFieldItem class.
推荐答案
您可能要创建一个subRepeater。
You may want to create a subRepeater.
<asp:Repeater ID="SubRepeater" runat="server" DataSource='<%# Eval("Fields") %>'>
<ItemTemplate>
<span><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
您也可以投你的领域
<%# ((ArrayFields)Container.DataItem).Fields[0].Name %>
最后,你可以做一个小CSV功能及功能写出你的领域
Finally you could do a little CSV Function and write out your fields with a function
<%# 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();
}
这篇关于绑定泛型列表到中继器 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文