我知道这是一匹经常被打败的死马,但我需要一些帮助来解决“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;
}
}
}
}
}
答案 0 :(得分:0)
您的headertemplate
标记正在包装整个转发器,这可能就是找不到HyperLink控件的原因。修复如下:
<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>