Cant bind Repeater to Custom Collection

Discussion in 'ASP .Net Web Controls' started by A Traveler, Oct 19, 2004.

  1. A Traveler

    A Traveler Guest

    Hi,

    I have a custom collection class i wrote, LineItemsCollection, which is a
    strongly typed collection of objects of my LineItem class.
    The LineItem class is a simple class with just a couple properties, ProdID,
    Description, Quantity and UPC. 3 strings and a long.
    The LineItemsCollection class inherits from
    System.Collections.Specialized.NameObjectCollectionBase.
    It also implements both IEnumerable and IList.

    I can bind a DataGrid to my collection with no problem. In the
    ItemCreated/ItemDataBound, the value of e.Item.DataItem is properly returned
    as a LineItem object. So i can do <%#
    DataBinder.Eval(Container,"DataItem.Quantity") %> and get back a quantity.

    However, if i try to bind to a Repeater, and i do the same thing, i get an
    error "DataBinder.Eval: 'System.String' does not contain a property with the
    name Quantity." I had had this problem with the datagrid when i first made
    my collection. After researching, i found out to inherit from IList, and it
    fixed it. But apparently the Repeater does its databinding differently than
    the DataGrid?

    Can anyone explain what the Repeater needs differently than the Datagrid in
    order to bind to a custom collection??

    Thanks in advance,
    - Arthur Dent.
     
    A Traveler, Oct 19, 2004
    #1
    1. Advertising

  2. A Traveler

    Karl Seguin Guest

    You might want to consider having your base class inherit from
    CollectionBase which will give you all the necessary flexability (not to
    mention allow you to bind to everything).

    Also, try the following syntax in your repeater:

    <%# ((LineItem)Container.DataItem).Quantity %>
    or in vb
    <%# ctype(Container.DataItem, LineItem).Quantity %>

    not sure if it'll sovle the problem though....

    you'll likely need to import the namespace of LineItem in your page with:
    <%@ Import Namespace="NamespaceContainerLineItem" %>

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "A Traveler" <> wrote in message
    news:...
    > Hi,
    >
    > I have a custom collection class i wrote, LineItemsCollection, which is a
    > strongly typed collection of objects of my LineItem class.
    > The LineItem class is a simple class with just a couple properties,

    ProdID,
    > Description, Quantity and UPC. 3 strings and a long.
    > The LineItemsCollection class inherits from
    > System.Collections.Specialized.NameObjectCollectionBase.
    > It also implements both IEnumerable and IList.
    >
    > I can bind a DataGrid to my collection with no problem. In the
    > ItemCreated/ItemDataBound, the value of e.Item.DataItem is properly

    returned
    > as a LineItem object. So i can do <%#
    > DataBinder.Eval(Container,"DataItem.Quantity") %> and get back a quantity.
    >
    > However, if i try to bind to a Repeater, and i do the same thing, i get an
    > error "DataBinder.Eval: 'System.String' does not contain a property with

    the
    > name Quantity." I had had this problem with the datagrid when i first made
    > my collection. After researching, i found out to inherit from IList, and

    it
    > fixed it. But apparently the Repeater does its databinding differently

    than
    > the DataGrid?
    >
    > Can anyone explain what the Repeater needs differently than the Datagrid

    in
    > order to bind to a custom collection??
    >
    > Thanks in advance,
    > - Arthur Dent.
    >
    >
     
    Karl Seguin, Oct 19, 2004
    #2
    1. Advertising

  3. A Traveler

    A Traveler Guest

    Yeah, that worked. I changed it to CollectionBase instead of
    NameObjectCollectionBase, and that apparently fixed it. Im not quite sure
    why though, because if i look at all the interfaces and bases in the
    objectbrowser, its all the same. At any rate it works now.

    I didnt want to do a hard-coded cast in the repeater code simply to keep it
    generic and usable for other collection types.

    Thanks.


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > You might want to consider having your base class inherit from
    > CollectionBase which will give you all the necessary flexability (not to
    > mention allow you to bind to everything).
    >
    > Also, try the following syntax in your repeater:
    >
    > <%# ((LineItem)Container.DataItem).Quantity %>
    > or in vb
    > <%# ctype(Container.DataItem, LineItem).Quantity %>
    >
    > not sure if it'll sovle the problem though....
    >
    > you'll likely need to import the namespace of LineItem in your page with:
    > <%@ Import Namespace="NamespaceContainerLineItem" %>
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "A Traveler" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> I have a custom collection class i wrote, LineItemsCollection, which is a
    >> strongly typed collection of objects of my LineItem class.
    >> The LineItem class is a simple class with just a couple properties,

    > ProdID,
    >> Description, Quantity and UPC. 3 strings and a long.
    >> The LineItemsCollection class inherits from
    >> System.Collections.Specialized.NameObjectCollectionBase.
    >> It also implements both IEnumerable and IList.
    >>
    >> I can bind a DataGrid to my collection with no problem. In the
    >> ItemCreated/ItemDataBound, the value of e.Item.DataItem is properly

    > returned
    >> as a LineItem object. So i can do <%#
    >> DataBinder.Eval(Container,"DataItem.Quantity") %> and get back a
    >> quantity.
    >>
    >> However, if i try to bind to a Repeater, and i do the same thing, i get
    >> an
    >> error "DataBinder.Eval: 'System.String' does not contain a property with

    > the
    >> name Quantity." I had had this problem with the datagrid when i first
    >> made
    >> my collection. After researching, i found out to inherit from IList, and

    > it
    >> fixed it. But apparently the Repeater does its databinding differently

    > than
    >> the DataGrid?
    >>
    >> Can anyone explain what the Repeater needs differently than the Datagrid

    > in
    >> order to bind to a custom collection??
    >>
    >> Thanks in advance,
    >> - Arthur Dent.
    >>
    >>

    >
    >
     
    A Traveler, Oct 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A Traveler

    Cant bind Repeater to Custom Collection

    A Traveler, Oct 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    7,500
    A Traveler
    Oct 19, 2004
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    652
    Joe Fallon
    Jul 3, 2005
  3. Steven Baggs
    Replies:
    5
    Views:
    43,383
    Steven Baggs
    Oct 18, 2005
  4. Nagaraj
    Replies:
    1
    Views:
    892
    Lionel B
    Mar 1, 2007
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    1,001
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page