Link to home
Start Free TrialLog in
Avatar of UncleAndy
UncleAndy

asked on

Access child element in nested repeaters (ASP.NET/C#)

Hi,

I have a page displaying bookmarks stored in a database, and organised in categories. The output has this form:

General bookmarks
     Bookmark1
     Bookmark2
     Bookmark3

Work
     Bookmark4
     Bookmark5

Funny stuff
      Bookmark6
      Bookmark7
      Bookmark8

I am displaying a checkbox next to each bookmark, and at the bottom of the list I have a delete button that should delete the bookmarks that have been checked by the user. The bookmarks and the categories are displayed using nested repeaters (where, naturally, the bookmarks are the child elements). When the button is clicked, it triggers an event that loops through the elements in the repeater, but I cant get it to loop through the child elements - only the parent (categories). The below code works fine for the parent elements (by using "parentrepeater" instead of "childrepeater")

for (int i = 0; i < childrepeater.Items.Count; i++) // error comes here - childrepeater seems to be unaccessible
{
      CheckBox CurrentCheckBox = new CheckBox();
      CurrentCheckBox = (CheckBox) childrepeater.Items[i].FindControl("Checkbox1");

      if (CurrentCheckBox.Checked)
      {
      // delete bookmark
      }
                  
}

But even though this code compiles, I get "Object reference not set to an instance of an object". The childrepeater is not initialised/instansiated, even though it's drawn on screen.

Anyone have an idea here? How do I access child elements in nested repeaters?

Andreas
Avatar of netjkus
netjkus

ASKER CERTIFIED SOLUTION
Avatar of netjkus
netjkus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UncleAndy

ASKER


Oh yes, there we go! Still a couple of issues I have to work out, but your tip helped me figure out how to get access to the child repeater. For others with a similar problem, here is my code:

// in the method triggered by the delete button
for (int i = 0; i < parentrepeater.Items.Count; i++)
{
      Repeater nestedRepeater = parentrepeater.Items[i].FindControl("childrepeater") as Repeater;

      for (int y = 0; y < nestedRepeater.Items.Count; y++)
      {
            CheckBox CurrentCheckBox = new CheckBox();
            CurrentCheckBox = (CheckBox) nestedRepeater.Items[y].FindControl("Checkbox1");

            if (CurrentCheckBox.Checked)
            {
                  // delete bokmark
            }
                  
      }
            
}

Thanks, netjkus! Problem solved within an hour!

Andreas