toro2k toro2k - 13 days ago 9
ASP.NET (C#) Question

Strongly typed data control throws compilation error: The name 'Item' does not exist

In Visual Studio 2015 I'm trying to use a strongly typed RadGrid as described in ASP.NET and Telerik documentation, but with no success.

When I try to run the following page I get a compilation error:

CS0103: The name 'Item' does not exist in the current context


WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebAppNamespace.WebForm1" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>

<telerik:RadGrid runat="server"
ItemType="ModelNamespace.SomeEntity"
SelectMethod="GetTheStuff"
AutoGenerateColumns="false">
<MasterTableView>
<Columns>

<telerik:GridTemplateColumn>
<ItemTemplate>
<%# Item.SomeProperty%>
</ItemTemplate>
</telerik:GridTemplateColumn>

</Columns>
</MasterTableView>
</telerik:RadGrid>


WebForm1.aspx.cs

using ModelNamespace;
using System.Linq;
using System.Web.UI;

namespace WebAppNamespace
{
public partial class WebForm1 : Page
{
SomeContext db = new SomeContext();
public IQueryable<SomeEntity> GetTheStuff()
{
return db.SomeEntity;
}
}
}


The code completion in Visual Studio doesn't work either.

Shoudn't
Item
be available when I use the
ItemType
attribute in the RadGrid control as described in the documentation? What am I missing?

Answer

For the Item object to be available in bind expressions, the ItemType property has to be specified on the MasterTableView element, i.e.:

<telerik:RadGrid runat="server"
    SelectMethod="GetTheStuff"
    AutoGenerateColumns="false">
    <MasterTableView ItemType="ModelNamespace.SomeEntity">
        <Columns>
        ...