0
我知道這是一個一直被毆打的死馬,但我需要一些協助解決「System.NullReferenceException:對象引用未設置爲對象實例」。問題。我已經去了很多的鏈接,但我是編程新手,我不能從這些建議的尾巴開始。HyperLink控件產生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;
}
}
}
}
}
它必須是'topNavigation'或'currentItem'。您正在檢查currentItem爲空,所以我相信'topNavigation'正在發生什麼...... – 2013-03-07 19:50:58
您可能需要對HyperLinkTopNavigation執行遞歸FindControl搜索 - 無論是該事件還是在該事件觸發時尚未加載。 – 2013-03-07 19:51:42
你確定currentItem有一個'Name'屬性嗎? – 2013-03-07 21:51:51