我想学习如何为IEnumerable LINQ集合绑定到一个中继器 [英] I am trying to learn how to bind an IEnumerable LINQ collection to a repeater

查看:123
本文介绍了我想学习如何为IEnumerable LINQ集合绑定到一个中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用LINQ从一个字符串数组这样下面赛车手的IEnumerable列表:

I have created an IEnumerable list of racing drivers using LINQ from a string array as such below:

string[] driverNames = {
                              "Lewis Hamilton", 
                              "Heikki Kovalainen",
                              "Felipe Massa",
                              "Kimi Raikkonen",
                              "Robert Kubica",
                              "Nick Heidfeld",
                              "Fernando Alonso",
                              "Nelson Piquet Jr",
                              "Jarno Trulli",
                              "Timo Glock",
                              "Sebastien Bourdais",
                              "Sebastien Buemi",
                              "Mark Webber",
                              "Sebastian Vettel",
                              "Nico Rosberg",
                              "Kazuki Nakajima",
                              "Adrian Sutil",
                              "Giancarlo Fisichella",
                              "Jenson Button",
                              "Rubens Barrichello"
                          };

IEnumerable<string> result = from driver in driverNames
                             orderby driver
                             select driver;



我只是简单说明了。

I am just keeping it simple for now.

我然后将其绑定到一个ASP.NET的GridView像这样如下:

I then bind it to a ASP.NET GridView like so below:

GV_CurrentF1Drivers.DataSource = result;
GV_CurrentF1Drivers.DataBind();

这工作正常。现在我想采取同样的输出(结果)并将其绑定到一个中继器,但不管是什么我尝试,我不能让中继器的工作,我想我缺少LINQ的一些关键的了解以及它如何与ASP.NET的工作原理

This works fine. Now I want to take the same output (result) and bind it to a repeater but no matter what I try I can not get the repeater to work and I think I am missing some key understanding of LINQ and how it works with ASP.NET.

以下是完整aspx页面展现在那里我有这么远。 ?可以请某人(轻轻如果可能的话)指导我回的路径

Below is the full aspx page to show where I have got to so far. Please can somebody (gently if possible) guide me back on to the path?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example1.aspx.cs" Inherits="Example1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="float: left;">
        <asp:GridView ID="GV_CurrentF1Drivers" runat="server" />
    </div>
    <div style="float: left;">
        <asp:Repeater ID="R_CurrentF1Drivers" runat="server">
            <ItemTemplate>
                <%# Eval("driver") %></ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>



我用下面的代码,结果到Repeater绑定:

I use the following code to bind the result to the Repeater:

R_CurrentF1Drivers.DataSource = result;
R_CurrentF1Drivers.DataBind();



我收到以下错误,当我尝试与中继器中运行的页面:

I get the following error when I try to run the page with the Repeater in:

异常详细信息: System.Web.HttpException:数据绑定:'System.String'不包含名为'司机'

Exception Details: System.Web.HttpException: DataBinding: 'System.String' does not contain a property with the name 'driver'.

推荐答案

你找回一个没有名字的字符串枚举。如果你想使用的属性名的驱动程序可以使匿名类型,并说:

You're getting back an enumerable of strings with no name. If you want to use the property name driver you can make an anonymous type and say:

var result = from driver in driverNames
             orderby driver
             select new { Driver = driver };



然后做数据绑定。

then do the databinding.

我相信你也可以评估(。)评估对象本身,而不是一个属性。另外,作为多个人说下面你可以使用<%#%的Container.DataItem方式>

I believe you can also Eval(".") to evaluate the object itself and not a property. Also as multiple people have said below you can use <%# Container.DataItem %>.

这篇关于我想学习如何为IEnumerable LINQ集合绑定到一个中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆