Home All Groups Group Topic Archive Search About

How to reach the object connected to repeater

Author
5 Apr 2006 11:18 AM
Karl-Inge Reknes
Hi

I have a repeater that is connected to an objectdatasource. I can reach the
item control like this.

foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox c= (CheckBox)i.FindControl("checkbox1");
      Label l = (Label)i.FindControl("label1");
}

The objectdatasource have information that i don’t want to show, but i need
to reach. One solution is to use controll with Visible set to false, but I
don’t like this solution.

Is it posible to reach the RepeaterItem dataobject?

I have tried this code with no sucsess:

foreach (RepeaterItem item in Repeater1.Items)
{
       CheckBox c= (CheckBox)i.FindControl("checkbox1");
       Label l = (Label)i.FindControl("label1");
       CustomerClass c = (CustomerClass)i.DataItem;
}

Is there anybody how have an suggestion?

Regard
Karl-Inge Reknes

Author
5 Apr 2006 1:07 PM
Phillip Williams
You can access the dataitem through the repeater items only before the
repeater is rendered during the ItemDataBound event like this:
  void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.SelectedItem)
        {
            //assuming the repeater is bound to a class named CustomerClass
            //each DataItem can be cast to that class and then the properties
            //can be accessed
            string FirstName = ((CustomerClass)e.Item.DataItem).FirstName;
        }
    }

But if you want to update the data after some user entries are done on the
repeater-rendered controls, e.g. the user changed the checkboxes state, then
you will have to retrieve the data records from the data store and locate the
individual rows using their primary keys.
Show quoteHide quote
"Karl-Inge Reknes" wrote:

> Hi
>
> I have a repeater that is connected to an objectdatasource. I can reach the
> item control like this.
>
> foreach (RepeaterItem item in Repeater1.Items)
> {
> CheckBox c= (CheckBox)i.FindControl("checkbox1");
>       Label l = (Label)i.FindControl("label1");
> }
>
> The objectdatasource have information that i don’t want to show, but i need
> to reach. One solution is to use controll with Visible set to false, but I
> don’t like this solution.
>
> Is it posible to reach the RepeaterItem dataobject?
>
> I have tried this code with no sucsess:
>
> foreach (RepeaterItem item in Repeater1.Items)
> {
>        CheckBox c= (CheckBox)i.FindControl("checkbox1");
>        Label l = (Label)i.FindControl("label1");
>        CustomerClass c = (CustomerClass)i.DataItem;
> }
>
> Is there anybody how have an suggestion?
>
> Regard
> Karl-Inge Reknes
>