在 ASP.NET 中将对象数组的 List 绑定到 ListView

发布于 2024-08-05 20:49:02 字数 528 浏览 3 评论 0原文

我正在绞尽脑汁去解决一个问题。我有一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九公里浅绿 2024-08-12 20:49:02

您的数据源不支持标准数据绑定。将其转换为名称值对,其中每个要绑定的项目都有一个名称和一个值。例如 Dictionary集合与此兼容。然后将你的 ListView 变成这样:

<asp:Label runat="server" ID="lblId"
    Text='<%# Eval("Key") %>'></asp:Label>

<asp:Label runat="server" ID="lblName"
    Text='<%# Eval("Value") %>'></asp:Label>

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 :

<asp:Label runat="server" ID="lblId"
    Text='<%# Eval("Key") %>'></asp:Label>

<asp:Label runat="server" ID="lblName"
    Text='<%# Eval("Value") %>'></asp:Label>
瞄了个咪的 2024-08-12 20:49:02

对象数组列表是存储项目的糟糕选择。您应该考虑使用表示项目的类,或者像 @Canavar 建议的那样使用字典。然后您就可以以更简洁的方式使用 Eval 方法。

也就是说,可以与您当前的设置绑定,尽管语法让我眼睛流血。

<asp:Label runat="server" ID="lblId"
    Text='<%# ((Object[])Container.DataItem)[0] %>' />
<asp:Label runat="server" ID="lblName"
    Text='<%# ((Object[])Container.DataItem)[1] %>' />

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.

<asp:Label runat="server" ID="lblId"
    Text='<%# ((Object[])Container.DataItem)[0] %>' />
<asp:Label runat="server" ID="lblName"
    Text='<%# ((Object[])Container.DataItem)[1] %>' />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文