4

I am setting the DataSource of my repeater to a List (MyProducts is a simple class, consisting of only get/setters).

After this and DataBind(), I can see in debugging mode that DataItem of each Repeater.Items is null. When making a postback and trying to update MyProducts, the Repeater.Items[n].DataItem is still null and Im not able to cast it, to do my work.

Why isn't DataItem set on each RepeaterItem, when I databind my repeater? I cant figure/Google it out. Every other aspect of my code works correctly (outputting data from MyProducts to aspx, using for instance:

<asp:TextBox runat="server" id="q" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity")%>'></asp:TextBox>

More code:

public class MyProducts
    {
        public string Number
        {
            get; set;
        }

        public decimal Price
        {
            get; set;
        }

        public decimal Quantity
        {
            get; set;
        }

        public decimal Total
        {
            get { return Quantity * Price; }
        }
    }

Generating:

public List<MyProducts> TheProducts
{
 get { // Invoking webservice, getting response as xml and converting it to a list of MyProducts }
}

My user control:

// Bind products in cart
r.DataSource = TheProducts;
r.DataBind();
// Debugging "r.Items[n].DataItem" now shows "null", eventhough all objects has been correctly binded

Edit #2, the debugging info. DataSource gets correctly loaded, but Repeater.Items[3].DataItem is null. It should have a value, right? screenshot


Edit #3, I get it now, I thought DataItem was always accessible when DataSource is set, and did not think of including the full code (I tried to access it in Page_Load).

After the user has edited an quantity value, I wanted to save the new quantity for a MyProducts. I solved it by placing a hiddenfield which holds the id of MyProducts, so that I can manually look it up and get the MyProducts object from there.

Thanks to emremp, Mark Avenius and all others who pitched in.

Certs
  • 85
  • 1
  • 1
  • 9
  • 1
    You might give us some code to work with... – Yves M. Oct 08 '10 at 08:36
  • I have edited my post and inserted more of the code. Though it's pretty basic, I dont think it will give any clarification. I had "hoped" I've missed something simple and someone could point it out for me. I find it very peculiar that I am able to bind MyProduct-objects but DataItem is null ... – Certs Oct 08 '10 at 08:47
  • What are you trying to achieve here? Does your "Quantity" get binded to the repeater? – bla Oct 08 '10 at 08:57
  • Yes, Quantity is a property of MyProducts which gets binded to a TextBox, so user can edit it. This works as expected. User should be able to change quantity, then click "Save" and have the code loop through r.Items, saving quantity for each MyProducts object – Certs Oct 08 '10 at 09:09
  • Try just <%#Eval("Quantity")%> – Kadir Oct 08 '10 at 11:35
  • Hm seem I dont do a very good job of explaining my problem :-). I have no issues getting Quantity (or any other values) on the front end. My problem is that my repeater seems to be correctly loaded and binded, but each RepeaterItem has value "null" in its DataItem (which I find strange, because front-end shows Quantity etc. nicely). – Certs Oct 08 '10 at 11:46

2 Answers2

5

For what purpose do you need the entire list? After the page is rendered, the list to which the Repeater is bound is not retained. If you need to keep it, you can put it into the session and retrieve it as necessary (on Page_Load, e.g.):

private List<MyProducts> _myList;
protected void Page_Load(object sender, EventArgs e)
{
    _myList = Session[MYPRODUCTSKEY] as IList;
}

You could also put this into your getter (check the session first, and invoke the webservice if necessary):

public List<MyProducts> TheProducts
{
 get 
 { 
     if(Session[MYPRODUCTSKEY] == null)
         Session[MYPRODUCTSKEY] = //invoke webservice
     return Session[MYPRODUCTSKEY] as List<MyProducts>;
 }
}
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
2

http://www.netnewsgroups.net/aspnet/t4049-question-repeater-dataitem.aspx

"DataItem is there only for the item-creation process, that is ItemCreated and ItemDataBound methods (ItemCreated when it happens due to call to DataBind)."

you can add ItemDataBound method and try to get DataItem.

emremp
  • 59
  • 2
  • Correct, but either way, once the binding events have fired, the original list is no longer retained. I think this was the intent of the OP. – Mark Avenius Oct 08 '10 at 13:46
  • I mean r_ItemDataBound(Object Sender, RepeaterItemEventArgs e) method not a new ItemDataBound method. – emremp Oct 08 '10 at 13:53