我試圖讓嵌套中繼器內的HyperLink控件顯示「Item No」字段值或當前Sitecore模板的「菜單標題」字段值。其中一個字段將存在於通過中繼器進行排序的所有模板中。但是我面臨的問題是獲取ItemDataBound函數的代碼來查找超鏈接控件。到目前爲止,代碼一直說它不存在。將2個Sitecore模板字段之一嵌入中繼器的超鏈接
這裏是中繼器:
<asp:Repeater ID="ProductCategories" runat="server" ItemType="Sitecore.Data.Items.Item" Visible="true" OnItemDataBound="ProductCategories_ItemDataBound">
<ItemTemplate>
<div class="left-nav-section clearfix">
<div class="left-nav-section-arrow clearfix"></div>
<a class="left-nav-sub-section-title" href="<%# Sitecore.Links.LinkManager.GetItemUrl(Item) %>" runat="server">
<%# Item.Fields["Menu Title"].Value %>
</a>
<div class="the-tiers">
<asp:Repeater runat="server" ID="CategoriesRepeater" ItemType="Sitecore.Data.Items.Item">
<ItemTemplate>
<asp:HyperLink ID="ItemURL" runat="server" NavigateUrl="<%# Sitecore.Links.LinkManager.GetItemUrl(Item) %>"/>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
這裏是爲中繼ItemDataBound事件:
protected void ProductCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var catItem = e.Item.DataItem as Item;
Repeater categoriesRepeater = e.Item.FindControl("CategoriesRepeater") as Repeater;
var catProducts = catItem.GetChildren();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink itemLink = (HyperLink)e.Item.FindControl("ItemURL");
foreach (Item catProduct in catProducts)
{
if (catProduct.TemplateID.ToString() == productListingTemplateID.ToString())
{
itemLink.Text = catProduct.Fields["Menu Title"].ToString();
}
else
{
itemLink.Text = catProduct.Fields["Item No"].ToString();
}
}
}
if (catProducts.ToList().Count == 0)
{
return;
}
categoriesRepeater.DataSource = catProducts;
categoriesRepeater.DataBind();
}
這裏是當我嘗試設置的文本屬性時出現的錯誤超鏈接:
Object reference not set to an instance of an object