views:

59

answers:

3

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.

A: 

Instead of just posting snippets out of context, can you post the entire class and the source for your user control?

Matthew Graybosch
A: 

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
A: 

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
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
I mean r_ItemDataBound(Object Sender, RepeaterItemEventArgs e) method not a new ItemDataBound method.
emremp