TheMethod
TheMethod

Reputation: 3001

'object' does not contain a definition error using repeater in ascx file

I have a table called "Entries" and am using LINQ to SQL for data access. I have created a stored procedure that fetches all entries. I've mapped that stored proc to a method in my data context, I've set the return type to "Entry".

I have created a method in my data access class that returns the results of this method call as List. I am binding the list to a repeater in a user control and attempting to access the property "EntryId" in my ascx file and am getting the following error:

'object' does not contain a definition for 'EntryId'

My method in my DataAccess class:

public static List<Entry> GetAllEntries()
{
    using (DataClassesDataContext context = new DataClassesDataContext())
    {
        return context.fbGetAllEntries().ToList();
    }
}

And in the user controls Page_PreRender event:

//my alias at the top of the file for clairification
using ASPWebControls = System.Web.UI.WebControls;

protected void Page_PreRender(System.Object sender, System.EventArgs e)
{
   rptEntries = new ASPWebControls.Repeater();

   rptEntries.DataSource = DataAccess.GetAllEntries();

   rptEntries.DataBind();
}

And in my ascx file:

<asp:Repeater ID = "rptEntries" runat = "server" >
    <ItemTemplate>
        <tr>
            <td><input type="checkbox" runat="server"  value="<%# Container.DataItem.EntryId %>"  /></td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

When I Googled this error I found it can happen when using anonymous types but that is not the case here. I am certain my repeater has a list of strongly typed objects bound to it. I've tried explicitally casting:

rptEntries.DataSource = (List<Entry>)DataAccess.GetAllEntries();

I am certain my Entry type has an EntryId as I can do this:

foreach (Entry en in (List<Entry>)rptEntries.DataSource)
{
   //when I step through I can see these are the correct values
   int i = en.EntryId;
}

I've tried importing my namespace in my ascx file:

<%@ Import NameSpace="Namespace.in.my.DataClass.cs" %> 

I'm not sure where things are going sideways, could anyone suggest what I am doing wrong? Any help would be appreciated, thanks!

Edit: missed a code block

Upvotes: 0

Views: 1085

Answers (2)

McGarnagle
McGarnagle

Reputation: 102743

Even though the items are strongly-typed, the repeater control exposes them as object-types in the ItemTemplate. You need to unbox it explicitly:

<%# ((Entry)Container.DataItem).EntryId %>

Note that ASP.Net 4.5 has a strongly-typed repeater that allows you to specify an ItemType property and removes the need for the unboxing.

Upvotes: 2

mreyeros
mreyeros

Reputation: 4379

Try the following in your data-binding statement for the checkbox:

 value="<%# Eval("EntryId") %>"

Upvotes: 1

Related Questions