简体   繁体   中英

Why can't i find Panel repeater item?

I keep getting an Object reference not set to an instance of an object error when I try to find a Panel control within a Repeater . But the other controls are all found fine? Can anyone see what is wrong here?

This is how I'm selecting the control:

Panel pnlSubCategories = (Panel)e.Item.FindControl("pnlSubCategories");

Markup:

<asp:Repeater ID="rptInnerCategories" runat="server" OnItemDataBound="rptCategories_OnItemDataBound">
  <ItemTemplate>
       <li id="liCategory" runat="server">
           <asp:HyperLink ID="lnkCategory" runat="server">
                <span><asp:Literal ID="litCategory" runat="server" Visible="true" /></span>
                <asp:Image ID="imgMan" runat="server" Visible="false" /></asp:HyperLink>

                <asp:Panel ID="pnlSubCategories" runat="server" Visible="false">
                  <ul>
                     <asp:Repeater ID="rptSubCategories" runat="server" Visible="false" OnItemDataBound="rptSubCategories_OnItemDataBound">
                      <ItemTemplate>
                        <li id="liSubCategory" runat="server">
                         <asp:HyperLink ID="lnkSubCategory" runat="server">
                          <span><asp:Literal ID="litSubCategory" runat="server" /></span></asp:HyperLink>
                        </li>
                       </ItemTemplate>
                      </asp:Repeater>
                  </ul>
                 </asp:Panel>
        </li>            
   </ItemTemplate>
</asp:Repeater>

Code behind:

if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
     Category category = (Category)e.Item.DataItem;
     HyperLink lnkCategory = (HyperLink)e.Item.FindControl("lnkCategory");
     Literal litCategory = (Literal)e.Item.FindControl("litCategory");
     HtmlGenericControl liCategory = (HtmlGenericControl)e.Item.FindControl("liCategory");
     Image imgMan = (Image)e.Item.FindControl("imgMan");

     Panel pnlSubCategories = (Panel)e.Item.FindControl("pnlSubCategories");
     Repeater subCategories = (Repeater)e.Item.FindControl("rptSubCategories");

     if (category.ParentCategoryId != 0)
     {
          pnlSubCategories.Visible = true; //Getting the error on this line

Thanks for any help.

Edit* What I've tried so far:

Panel pnlSubCategories = (Panel)liCategory.Controls[0].FindControl("pnlSubCategories");

Panel pnlSubCategories = (Panel)liCategory.Controls[1].FindControl("pnlSubCategories");

Panel pnlSubCategories = (Panel)Page.FindControl("pnlSubCategories");

Panel pnlSubCategories = (Panel)e.Item.FindControl("pnlSubCategories");

But I still get the same error...

Edit 2*

I commented out the Panel control and it can't find the Repeater subCategories underneath it either? Something has gone horribly wrong here.......

Edit 3*

Code Behind and Markup

The problem IS that you are using the same method for different repeaters.

In you last update you posted the whole markup and code, and if you search through the markup you can find the rptCategories_OnItemDataBound used on several repeaters:

<asp:Repeater ID="rptCategories" runat="server" OnItemDataBound="rptCategories_OnItemDataBound">

and

<asp:Repeater ID="rptInnerCategories" runat="server" OnItemDataBound="rptCategories_OnItemDataBound">

According to documentation of FindControl() method on msdn , it only finds a control if it's a direct child of an element you're searching in.

This is not true in your case and that's why you can't find the control this way. You should find liCategory , then lnkCategory and then pnlSubCategories .

So, try this code:

Control liElement = (Control)e.Item.FindControl("liCategory");
Panel pnlSubCategories = (Panel)liElement .FindControl("pnlSubCategories");

EDIT

I've corrected the code snippet, it should be ok now :).

Alternatively, you can write a recursive version of the FindControl() method and use it instead. However, this should be rather used when you want the solution to be independent from the page structure. You can find some sample implementation of this kind of recursive method here: http://geekswithblogs.net/QuandaryPhase/archive/2009/05/06/asp.net-recursive-findcontrol-amp-extension-methods.aspx .

用这个

Panel pnlSubCategories = (Panel)liCategory.FindControl("pnlSubCategories");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM