簡體   English   中英

為什么找不到面板中繼器項目?

[英]Why can't i find Panel repeater item?

嘗試在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 但是其他控件都沒問題嗎? 誰能看到這里出什么問題了?

這就是我選擇控件的方式:

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 *

背后的代碼標記

問題是您對不同的轉發器使用了相同的方法。

在最后一次更新中,您發布了整個標記和代碼,如果您搜索標記,則可以找到在多個轉發器上使用的rptCategories_OnItemDataBound

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

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

根據msdn上FindControl()方法的文檔,如果它是您要搜索的元素的直接子代,則它只會找到一個控件。

在您的情況下,這是不正確的,這就是為什么您無法以這種方式找到控件的原因。 您應該找到liCategory ,然后是lnkCategory ,然后是pnlSubCategories

因此,請嘗試以下代碼:

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

編輯

我已經更正了代碼片段,現在應該可以了:)。

另外,您可以編寫FindControl()方法的遞歸版本,並改用它。 但是,當您希望解決方案獨立於頁面結構時,應該使用此方法。 您可以在此處找到這種遞歸方法的一些示例實現: 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