在 ASP.NET 中将对象数组的 List 绑定到 ListView
我正在绞尽脑汁去解决一个问题。我有一个返回 List
的方法。
列表中的每个 object[]
包含以下内容:
object[0]=Id;
object[1]=Name;
现在,我正在寻找一种方法将此列表绑定到自定义 ItemTemplate
中的 ListView,如下所示:
<asp:Label runat="server" ID="lblId"
Text="Here want to do an Eval/Bind for object[0]"></asp:Label>
<asp:Label runat="server" ID="lblName"
Text="Here want to do an Eval/Bind for object[1]"></asp:Label>
任何建议将不胜感激。
I am breaking my head to fix an issue. I have a method that returns a List<Object[]>
.
Each object[]
in the List contains the following:
object[0]=Id;
object[1]=Name;
Now, I am looking for a way to bind this List to a ListView in a custom ItemTemplate
which would look as follows:
<asp:Label runat="server" ID="lblId"
Text="Here want to do an Eval/Bind for object[0]"></asp:Label>
<asp:Label runat="server" ID="lblName"
Text="Here want to do an Eval/Bind for object[1]"></asp:Label>
Any suggestions will be deeply appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的数据源不支持标准数据绑定。将其转换为名称值对,其中每个要绑定的项目都有一个名称和一个值。例如 Dictionary 集合与此兼容。然后将你的 ListView 变成这样:
Your datasource is not capable for standard databinding. Convert it to a name value pair, which will have a name and a value for each item that will be binded. For example Dictionary<string, string> collection is compatible for this. And then just turn your ListView to this :
对象数组列表是存储项目的糟糕选择。您应该考虑使用表示项目的类,或者像 @Canavar 建议的那样使用字典。然后您就可以以更简洁的方式使用 Eval 方法。
也就是说,可以与您当前的设置绑定,尽管语法让我眼睛流血。
A list of object arrays is a poor choice to store items in. You should consider using a class that represents the item, or a Dictionary as @Canavar suggested. Then you would be able to use the Eval method in a cleaner fashion.
That said, it is possible to bind with your current setup, although the syntax makes my eyes bleed.