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

Posted on 2004-09-21
Last Modified: 2011-04-14

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

General bookmarks


Funny stuff

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?

Question by:UncleAndy
  • 2

Expert Comment

ID: 12112229

Accepted Solution

netjkus earned 300 total points
ID: 12112244
                      Repeater nestedRepeater = e.Item.FindControl("NestedRepeater")  as Repeater;
                        if(nestedRepeater != null)
                            nestedRepeater.DataSource =     dv.CreateChildView("CustomerOrders");

Author Comment

ID: 12112549

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!


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question