views:

480

answers:

1

Hello,

I have a repeater control bound to a collection of objects. When I fire off the button_onclick event, I need to have access to the dataitem to get the object properties. Here is what I have and my question is how do I access the underlying objects in a repeater in a button_onclick event

protected void OKButton_Click(object sender, EventArgs e)
{
    try
    {
         string selectedValue = Request.Form["repeaterRadioButton"];
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
            {
                MyObject myObject = (MyObject)item.DataItem;
                if (!string.IsNullOrEmpty(selectedValue) && selectedValue == myObject.MyProperty)
                {
                     //stuff in here
                } ... rest of code
+1  A: 

Hey,

The dataitem is not retained; it's only used to bind the initial interface, unless you rebind the repeater on every page load. Then you need to give the button a commandname value, and tap into the repeater.itemCommand, which will give you access to the repeater item, where the dataitem property lives.

EDIT: If you need to access items within the repeater, you can do:

foreach (var item in this.rpt.Items)
{
   if (item.DataItem != null) {
      //Do something
   }
}

Are you trying to access one row or the collection of rows?

HTH.

Brian
Hi, I'm trying this but if I leave the button outside the repeater, it doesn't hit the ItemCommand event and if I put the button in the repeater footer, it still says e.Item.DataItem is null
Ok, what I described was the button inside the repeater item template... I thought that's what you were looking for, sorry about that. the footer doesn't have a data item, so it will always be null... are you looking to access all the rows, or just one? You can access the items collection of the repeater for this, as edited above.
Brian