0

これは何度も何度も殴られた死んだ馬であることを私は知っていますが、「System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません」を解決するための支援が必要です。問題。私はたくさんのリンクに行きましたが、私はプログラミングに不慣れで、これらの提案のために尻尾から頭を作ることはできません。

エラーは47行目で発生します。

Line 45: HyperLink topNavigation = (HyperLink)e.Item.FindControl("HyperLinkTopNavigation");
Line 46: //Use Sitecore API to get the link to the Item and upadte the href property of link
Line 47: topNavigation.NavigateUrl = LinkManager.GetItemUrl(currentItem);
Line 48: //Assign name to the link
Line 49: topNavigation.Text = currentItem.Name;

私のascxコードは次のとおりです。

<%@ Control Language="c#" AutoEventWireup="true" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"  Inherits="Layouts.Topnavigation.TopnavigationSublayout" CodeFile="~/layouts/TopNavigation.ascx.cs"  Debug="true" %>

<%@ register TagPrefix="sc" Namespace="Sitecore.Web.UI.WebControls" Assembly="Sitecore.Kernel" %>
    <asp:repeater runat="server" id="RepeaterTopNavigation" onitemdatabound="RepeaterTopNavigation_ItemDataBound">
<headertemplate>
<ul>
<li><a href="home.aspx">Home</a></li>


<itemtemplate>
<li>
<asp:hyperlink id="HyperLinkTopNavigation" runat="server"></asp:hyperlink>
</li>
</itemtemplate>

<footertemplate>

</footertemplate></ul></headertemplate>
</asp:repeater>    

私の背後にあるコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Sitecore.Links;


namespace Layouts.Topnavigation {

    /// <summary>
    /// Summary description for TopnavigationSublayout
    /// </summary>
  public partial class TopnavigationSublayout : System.Web.UI.UserControl 
{
          protected void Page_Load(object sender, EventArgs e)
{
//get the home item, this is hardcoded but you can define it in web.config
Item home = Sitecore.Context.Database.GetItem("/sitecore/content/home");
//get all children from home using Sitecore API
Item[] children = home.Children.ToArray();
//Bind the children to repeater
RepeaterTopNavigation.DataSource = children;
RepeaterTopNavigation.DataBind();

}

public void RepeaterTopNavigation_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//gets the current data item from RepeaterItemEventsArgs and cast it as a Sitecore Item
Item currentItem = (Item)e.Item.DataItem;

//check if it is not null, safety first
if (currentItem != null)
{

//check if it is coming from Item or Alternating Item template
if (e.Item.ItemType == ListItemType.Item || 
          e.Item.ItemType == ListItemType.AlternatingItem)
{

//Find the HyperLink control that has been defined in repeater
HyperLink topNavigation = (HyperLink)e.Item.FindControl("HyperLinkTopNavigation");
//Use Sitecore API to get the link to the Item and upadte the href property of link
topNavigation.NavigateUrl = LinkManager.GetItemUrl(currentItem);
//Assign name to the link
topNavigation.Text = currentItem.Name;

}
}
}
}
}
4

1 に答える 1

0

タグがリピーター全体をラップしているため、 HyperLinkheadertemplateコントロールが見つからない可能性があります。次のように修正します。

<asp:repeater runat="server" id="RepeaterTopNavigation" onitemdatabound="RepeaterTopNavigation_ItemDataBound">
<headertemplate>
<ul>
<li><a href="home.aspx">Home</a></li>
</headertemplate>
<itemtemplate>
<li>
<asp:hyperlink id="HyperLinkTopNavigation" runat="server"></asp:hyperlink>
</li>
</itemtemplate>

<footertemplate>
</ul>
</footertemplate>
</asp:repeater>    
于 2013-03-07T20:12:18.453 に答える