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”)希望它能工作我也试过了。运气不好。没有其他中继器使用同一事件。您给出了错误的索引,正确的索引为3。请使用此pnlSubCategories=(面板)liCategory.Controls[3]。FindControl(“pnlSubCategories”);我尝试了大量索引,但仍然找不到它…好的,那么不要给出索引,只要使用Panel pnlSubCategories=(Panel)liCategory.FindControl(“pnlSubCategories”)希望它能工作我也试过了。不走运。没有其他中继器使用相同的事件。我将尝试此操作,但没有hpyerlinkElement,因为面板不包含在其中。不起作用。我想我已经试过了。我并不怀疑你,但是如果它只在你正在搜索的元素的直接子元素中找到一个控件。
是真的,那么为什么它能够在liCategory
中找到其他控件,比如imgMan
等等?我复制了您的标记,并能够以这种方式在rptcontegories\u onitemdata-bound
方法中找到面板。你会犯什么样的错误?是的。至于FindControl的非递归行为,我想我已经看到了这是一个问题。在您的案例中,更有趣的是,即使您的代码隐藏程序在我制作的示例应用程序中也能找到这个面板。在rptcombitions\u OnItemDataBound
中添加第一行:stringparentid=(发送方作为控件).ID代码>放置断点,并确保在出现此错误时它是rptInnerCategories
。rptInnerCategories\u OnItemDataBound
用于这两种情况<