Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我找不到面板中继器项目?_C#_Asp.net_Controls_Repeater_Panel - Fatal编程技术网

C# 为什么可以';我找不到面板中继器项目?

C# 为什么可以';我找不到面板中继器项目?,c#,asp.net,controls,repeater,panel,C#,Asp.net,Controls,Repeater,Panel,当我试图在中继器中查找面板控件时,我不断得到一个未设置为对象实例的对象引用。但是其他的控制都很好?有人能看出这里出了什么问题吗 这是我选择控件的方式: Panel pnlSubCategories = (Panel)e.Item.FindControl("pnlSubCategories"); 标记: <asp:Repeater ID="rptInnerCategories" runat="server" OnItemDataBound="rptCategories_OnItemData

当我试图在
中继器
中查找
面板
控件时,我不断得到一个未设置为对象实例的
对象引用。但是其他的控制都很好?有人能看出这里出了什么问题吗

这是我选择控件的方式:

Panel pnlSubCategories = (Panel)e.Item.FindControl("pnlSubCategories");
标记:

<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>
谢谢你的帮助

编辑*到目前为止我尝试的内容:

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”)

但我还是犯了同样的错误

编辑2*

我注释掉了
面板
控件,它也找不到它下面的
中继器子类别
?这里出了严重的问题

编辑3*

使用此

Panel pnlSubCategories = (Panel)liCategory.FindControl("pnlSubCategories");
用这个

Panel pnlSubCategories = (Panel)liCategory.FindControl("pnlSubCategories");
根据,它仅在控件是您正在搜索的元素的直接子控件时才能找到该控件

这在你的情况下是不正确的,这就是为什么你不能通过这种方式找到控制。您应该找到
liCategory
,然后找到
lnkCategory
,然后找到
pnlSubCategories

因此,请尝试以下代码:

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

我已经更正了代码片段,现在应该可以了:)

或者,您可以编写一个递归版本的
FindControl()
方法,并改用它。但是,当您希望解决方案独立于页面结构时,应该使用此选项。您可以在这里找到这种递归方法的一些示例实现:。

根据,它仅在您正在搜索的元素的子元素中查找控件

这在你的情况下是不正确的,这就是为什么你不能通过这种方式找到控制。您应该找到
liCategory
,然后找到
lnkCategory
,然后找到
pnlSubCategories

因此,请尝试以下代码:

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

我已经更正了代码片段,现在应该可以了:)


或者,您可以编写一个递归版本的
FindControl()
方法,并改用它。但是,当您希望解决方案独立于页面结构时,应该使用此选项。您可以在这里找到这种递归方法的一些示例实现:。

问题是您对不同的中继器使用相同的方法


在上一次更新中,您发布了整个标记和代码,如果您搜索标记,您可以找到在多个转发器上使用的
rptcontegories\u OnItemDataBound

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


问题在于,您对不同的中继器使用相同的方法


在上一次更新中,您发布了整个标记和代码,如果您搜索标记,您可以找到在多个转发器上使用的
rptcontegories\u OnItemDataBound

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




我想不是这样的。我早些时候把它设为真,结果没有什么不同。另外,我在这个页面上还有其他面板,它们具有相同的属性,但都很好。添加null检查将解决错误,但实际上找不到我想要执行的控件。据我所知,visible属性不会影响后面的代码。。。只有当visible设置为false时,标记代码才不会被呈现。我不这么认为。我早些时候把它设为真,结果没有什么不同。另外,我在这个页面上还有其他面板,它们具有相同的属性,但都很好。添加null检查将解决错误,但实际上找不到我想要执行的控件。据我所知,visible属性不会影响后面的代码。。。如果visible设置为false,则只有标记代码不会呈现。如果给定的索引错误,则正确的索引为3。请使用此pnlSubCategories=(面板)liCategory.Controls[3]。FindControl(“pnlSubCategories”);我尝试了大量索引,但仍然找不到它…好的,那么不要给出索引,只要使用
Panel pnlSubCategories=(Panel)liCategory.FindControl(“pnlSubCategories”)Panel pnlSubCategories=(Panel)liCategory.FindControl(“pnlSubCategories”)它只在你正在搜索的元素的直接子元素中找到一个控件。
是真的,那么为什么它能够在
liCategory
中找到其他控件,比如
imgMan
等等?我复制了您的标记,并能够以这种方式在
rptcontegories\u onitemdata-bound
方法中找到
面板。你会犯什么样的错误?是的。至于FindControl的非递归行为,我想我已经看到了这是一个问题。在您的案例中,更有趣的是,即使您的代码隐藏程序在我制作的示例应用程序中也能找到这个
面板。在
rptcombitions\u OnItemDataBound
中添加第一行:
stringparentid=(发送方作为控件).ID放置断点,并确保在出现此错误时它是
rptInnerCategories
rptInnerCategories\u OnItemDataBound
用于这两种情况<