?
Solved

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

Posted on 2004-09-21
3
Medium Priority
?
2,988 Views
Last Modified: 2011-04-14
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
0
Comment
Question by:UncleAndy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:netjkus
ID: 12112229
0
 
LVL 2

Accepted Solution

by:
netjkus earned 1200 total points
ID: 12112244
                      Repeater nestedRepeater = e.Item.FindControl("NestedRepeater")  as Repeater;
                        if(nestedRepeater != null)
                        {
                            nestedRepeater.DataSource =     dv.CreateChildView("CustomerOrders");
                            nestedRepeater.DataBind();
                        }
0
 
LVL 1

Author Comment

by:UncleAndy
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!

Andreas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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