Object reference not set to an instance of an object
当我尝试Panel
在Repeater
. 但是其他控件都发现没问题?谁能看到这里有什么问题?
这就是我选择控件的方式:
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>
后面的代码:
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
谢谢你的帮助。
编辑*到目前为止我尝试过的内容:
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*
我注释掉了Panel
控件,它也找不到Repeater subCategories
下面的控件?这里发生了可怕的错误......
编辑 3*