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'.
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 %>
.
Change <%# Eval("driver") %>
to
<%# Container.DataItem %>
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.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With