Asp.net 超链接控件生成System.NullReferenceException
我知道这是一匹被反复击败的死马,但我需要一些帮助来解决“System.NullReferenceException:对象引用未设置为对象实例”问题。我已经浏览了很多链接,但我对编程还不熟悉,所以我不能完全接受这些建议 错误发生在第47行:Asp.net 超链接控件生成System.NullReferenceException,asp.net,object,instance,Asp.net,Object,Instance,我知道这是一匹被反复击败的死马,但我需要一些帮助来解决“System.NullReferenceException:对象引用未设置为对象实例”问题。我已经浏览了很多链接,但我对编程还不熟悉,所以我不能完全接受这些建议 错误发生在第47行: Line 45: HyperLink topNavigation = (HyperLink)e.Item.FindControl("HyperLinkTopNavigation"); Line 46: //Use Sitecore API to get the
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;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用Sitecore.Data.Items;
使用Sitecore.Links;
名称空间布局.Topnavigation{
///
///TopnavigationSublayout的摘要说明
///
公共部分类TopnavigationSublayout:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
//获取主项,这是硬编码的,但您可以在web.config中定义它
itemhome=Sitecore.Context.Database.GetItem(“/Sitecore/content/home”);
//使用Sitecore API从家中获取所有儿童
Item[]children=home.children.ToArray();
//把孩子们绑在复读机上
RepeaterTopNavigation.DataSource=子级;
RepeaterTopNavigation.DataBind();
}
public void RepeaterTopNavigation_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
//从RepeaterItemEventsArgs获取当前数据项,并将其强制转换为Sitecore项
Item currentItem=(Item)e.Item.DataItem;
//检查是否不为空,安全第一
如果(currentItem!=null)
{
//检查它是否来自项目或交替项目模板
如果(e.Item.ItemType==ListItemType.Item ||
e、 Item.ItemType==ListItemType.AlternatingItem)
{
//查找已在repeater中定义的超链接控件
HyperLink topNavigation=(HyperLink)e.Item.FindControl(“HyperLinkTopNavigation”);
//使用Sitecore API获取指向该项目的链接,并更新链接的href属性
topNavigation.NavigateUrl=LinkManager.GetItemUrl(currentItem);
//为链接指定名称
topNavigation.Text=currentItem.Name;
}
}
}
}
}
您的headertemplate
标记正在包装整个中继器,这可能是找不到超链接控件的原因。像这样修复它:
<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>
必须是topNavigation
或currentItem
。您正在检查currentItem是否为null,因此我相信topNavigation
…您可能需要对HyperLinkTopNavigation执行递归FindControl搜索-要么这样,要么在该事件触发时尚未加载。您确定currentItem具有名称
属性吗?