0

内でコントロールObject reference not set to an instance of an objectを見つけようとすると、エラーが発生し続けます。しかし、他のコントロールはすべて問題ありませんか? ここで何が悪いのか誰にもわかりますか?PanelRepeater

これは私がコントロールを選択する方法です:

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*

コード ビハインドマークアップ

4

3 に答える 3

5

問題は、異なるリピーターに対して同じ方法を使用していることです。

前回の更新では、マークアップとコード全体を投稿しました。マークアップを検索すると、rptCategories_OnItemDataBoundいくつかのリピーターで使用されていることがわかります。

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

<asp:Repeater ID="rptInnerCategories" runat="server" OnItemDataBound="rptCategories_OnItemDataBound">
于 2012-08-15T12:04:15.327 に答える
1

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で見つけることができます。

于 2012-08-15T11:20:01.953 に答える
0

これを使って

Panel pnlSubCategories = (Panel)liCategory.FindControl("pnlSubCategories");
于 2012-08-15T10:07:49.570 に答える