Budda
Budda

Reputation: 18353

How to get item bound to control by repeater?

I have 'BasketControl' control inside of repeater:

<asp:Repeater runat="server" ID="rptItems">
    <ItemTemplate>
        <uc:BasketControl runat="server" ID="ucBasket" />
    </ItemTemplate>
</asp:Repeater>

The list of BasketObject is bound the repeater DataSource property.

public class MyParentControl : System.Web.UI.UserControl
{
    void Page_Load(object sender, EventArgs e) {
        if(!IsPostback) {
            rptItems.DataSource = ...; // List<BasketObject>
            rptItems.DataBind();
        }
    }
}

How can I access 'BasketObject' inside of BasketControl? What I want is to have access to BasketObject inside of class BasketControl, for example inside of the 'Page_Load' method.

public class BasketControl : System.Web.UI.UserControl
{
    void Page_Load(object sender, EventArgs e) {
        // here I want access BasketObject
        BasketObject basket = .... as BasketObject;
    }
}

Should be simple, but can't find any tips in Google...

P.S. Should I handle 'OnItemDataBound' repeater event and assign data object to control? Or there is quicker/easier way?

Thanks

Upvotes: 1

Views: 3222

Answers (1)

rossipedia
rossipedia

Reputation: 59437

Usually how I handle those kind of things is by handling the ItemDatabound event.

So say you have this markup:

<asp:Repeater runat="server" ID="rptItems">
    <ItemTemplate>
        <uc:BasketControl runat="server" ID="ucBasket" />
    </ItemTemplate>
</asp:Repeater>

The code would look something like this:

protected override void OnInit(EventArgs e) {
    base.OnInit(e);

    this.rptItems += new RepeaterItemEventHandler(rptItems_ItemDatabound);
}

void Page_Load(object sender, EventArgs e) {
    if(!IsPostback) {
        rptItems.DataSource = ...; // List<BasketObject>
        rptItems.DataBind();
    }
}

void rptItems_ItemDatabound(object sender, RepeaterItemEventArgs e) {
    BasketControl ucBasket = e.Item.FindControl("ucBasket") as BasketControl;
    BasketObject basket = e.Item.DataItem as BasketObject;

    // Use datasource/databind, but you could just as easily use a property/method of BasketControl
    // Like so: ucBasket.LoadBasket(basket);
    ucBasket.DataSource = basket;
    ucBasket.DataBind();
}

Hope that's clear. Of course, you could also use AutoEventWireup="true" and put the name of the event handler in the markup of the Repeater. But the basic idea is still the same.

EDIT In response to your clarification, you're going to have to explicitly wire them up some how. There isn't an automatic way of doing it.

I would suggest exposing a Basket property on the BasketControl. Then you could do something like this:

<%@ Import Namespace="Your WebApp Namespace Here" %>

<asp:Repeater runat="server" ID="rptItems">
    <ItemTemplate>
        <uc:BasketControl runat="server" ID="ucBasket" Basket='<%# (Basket)Container.DataItem %>' />
    </ItemTemplate>
</asp:Repeater>

And you wouldn't need to handle the ItemDatabound event. If there's another way of doing this, I'm not sure what it is.

Upvotes: 1

Related Questions