Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am trying to learn how to bind an IEnumerable LINQ collection to a repeater

Tags:

c#

asp.net

linq

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.

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

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

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.

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>

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:

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

like image 980
Ian Roke Avatar asked May 06 '09 20:05

Ian Roke


4 Answers

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.

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 %>.

like image 132
Orion Adrian Avatar answered Oct 27 '22 01:10

Orion Adrian


Change <%# Eval("driver") %> to

<%# Container.DataItem %>
like image 40
mmx Avatar answered Oct 27 '22 01:10

mmx


try

<%# Container.DataItem.ToString() %>

The eval is looking for a property named driver but you have the actuall driver name as a string. It'd be like calling "drivername".DriverName. The enumeration of your collection is working with the actual items. If you actually had a collection like IList drivers, then that would work.

like image 35
Joshua Belden Avatar answered Oct 27 '22 01:10

Joshua Belden


I realize this is an old question but I wanted to point out that a string array already is IEnumerable<string> and you could just bind the array directly to the repeater using Mehrdad's answer:

<%# Container.DataItem %>
like image 28
Jamie Ide Avatar answered Oct 27 '22 00:10

Jamie Ide