简体   繁体   English

为什么找不到面板中继器项目?

[英]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 . 尝试在Repeater查找Panel控件时,我一直收到Object reference not set to an instance of an object错误Object reference not set to an instance of an objectObject reference not set to an instance of an object 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* 编辑2 *

I commented out the Panel control and it can't find the Repeater subCategories underneath it either? 我注释掉了Panel控件,它下面也找不到Repeater subCategories Something has gone horribly wrong here....... 这里出了些可怕的事情.......

Edit 3* 编辑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: 在最后一次更新中,您发布了整个标记和代码,如果您搜索标记,则可以找到在多个转发器上使用的rptCategories_OnItemDataBound

<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. 根据msdn上FindControl()方法的文档,如果它是您要搜索的元素的直接子代,则它只会找到一个控件。

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 . 您应该找到liCategory ,然后是lnkCategory ,然后是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. 另外,您可以编写FindControl()方法的递归版本,并改用它。 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 . 您可以在此处找到这种递归方法的一些示例实现: http : //geekswithblogs.net/QuandaryPhase/archive/2009/05/06/asp.net-recursive-findcontrol-amp-extension-methods.aspx

用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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