views:

28

answers:

1

Hello

I have a repeater that is looping a user control, like this:

                <asp:Repeater ID="repItems" runat="server" EnableViewState="false" 
                OnItemCommand="repItems_ItemCommand">
            <ItemTemplate>
                <dmg:confirmItem runat="server" 
                OnDataBinding="confirmitemItem_DataBinding"  
                Basket="<%# Container.DataItem %>" />
            </ItemTemplate>

            </asp:Repeater>

My code behind looks like this:

  public void BindItems(List<ShopBasket> baskets)
    {
        _baskets = baskets;
        repItems.DataSource = baskets;
        repItems.DataBind();
    }

My custom user control looks like this:

public ShopBasket Basket;
        protected void Page_Load(object sender, EventArgs e)
        {

            imgItem.ImageUrl = ShopImagePath + Basket.ImageFilename;
            ...etc...
        }

All works brilliantly the first time around, the basket items are bound to Basket objects and everything is great.

However, when I receive an ItemCommand from my repeater, and update the basket contents (Note: No adding or removing is done here, just updates the quantity) then I rebind to see the latest values, and BOOM! Null reference - no Basket object in the user control Page_Load. This is despite tracing through to see that the BindItems() method is called as usual, and the Baskets are there.

I presume this has something to do with the life cycle but it has me beat.

Any ideas?

Thanks Duncan

+2  A: 

Hey,

It's a little dangerous to have a public field store an item being bound, especially when you have multiple items. A safer way to do it is to extract the Basket as the DataItem bound to the repeater, and do something with it that way in the repeater ItemCommand event. ItemDataBound would be even safer as you know the basket would be existing (since it's data bound), Page_Load is not a safe option here...

HTH.

Brian
Thanks for getting back to me. I'm having difficulty understanding (maybe because it is 7.15pm!) as I don't see what is wrong with what I'm doing. Bear in mind that the Page_Load outlined above is on my user control... so that should have received the Basket from the repeater's data bind by now? Unless that is where I'm going wrong?
Duncan
When are you binding again? Basket isn't persisted across page postbacks, and from what I'm seeing, the rebind needs to take place before page load fires, which it probably isn't.
Brian
I am binding on PageLoad at the moment, just like http://weblogs.asp.net/infinitiesloop/archive/2006/10/16/TRULY-Understanding-Dynamic-Controls-_2800_Part-4_2900_.aspx tells me to do!
Duncan
Okay cracked it. Thanks - just couldn't get my head round it last night. You're right, it should be happening in ItemDataBound on the repeater - I then can find my user control and pass the dataItem to it. I've written it up here if anyone finds it useful - http://www.binaryforge-software.com/Blog/2010/10/30/RepeatersUserControlsDataBindingAndEvents.aspx
Duncan