如何链接项目绑定到Sitecore的一个中继器 [英] How to bind link items to a Repeater in Sitecore

查看:135
本文介绍了如何链接项目绑定到Sitecore的一个中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的内容树如下:

 内容
  - 新闻资讯
  -Article1
  -Article2
  -Article3

每篇文章是数据模板 - 新闻文章,这是因为:

 新闻文章
  文章 - (场部分)
    相关文章 - (多重表 - 与数据源为'/内容/新闻列表)

每个文章有其他2篇,作为其相关文章。对于如:第一条有相关的文章2及3等

标记:

 < H3>相关文章:LT; / H3 GT&;
< ASP:直放站ID =rpArticles=服务器的ItemType =Sitecore.Data.Items.Item>
 <&HeaderTemplate中GT;
    < UL类=relatedArticles>
 < / HeaderTemplate中>
 <&ItemTemplate中GT;
    <立GT;
      < SC:链接字段=<%#Item.Paths.FullPath%GT; =服务器项=<%#的Container.DataItem%GT;>
          < SC:文本字段=标题=服务器/>
      < / SC:LINK>
    < /李>
 < / ItemTemplate中>
 < FooterTemplate>
    < / UL>
 < / FooterTemplate>
< / ASP:直放站>

code:

 私人无效的Page_Load(对象发件人,EventArgs的发送)
 {
   MultilistField relatedArticles = Sitecore.Context.Item.Fields [相关文章];
   rpArticles.DataSource = relatedArticles.TargetIDs.Select(ID => Sitecore.Context.Database.GetItem(ID));
   rpArticles.DataBind();
 }

以上标记为这里结果的解决方案
标题是字段的名称(例如:第一条,第二条等。)

当我浏览新闻文章1,相关文章应该是2及3,但输出不正确&放大器;还没有锚标记。只是纯文本。

什么是错误的,我code。

输出:


  

相关文章结果
  第一条结果
  第一条



解决方案

不知道为什么你改变你原来的问题,因为它几乎是正确的。由于在通领域不指定要链接的项目不能使用 SC:链接控制,反而是简单得多或者使用的 ASP.Net超链接控制这是从code势必在Item_Bound事件或干脆背后该项目的URL追加到锚链接:

 <&ItemTemplate中GT;
    <立GT;
        &所述; A HREF =&下;%#Sitecore.Links.LinkManager.GetItemUrl((Sitecore.Data.Items.Item)的Container.DataItem)%>中与GT;
            < SC:文本字段=标题=服务器项=<%#的Container.DataItem%GT;/>
        &所述; / A>
    < /李>
< / ItemTemplate中>

The content tree is as follows:

content
 -News Listing
  -Article1
  -Article2
  -Article3

Each of the Articles is of a Data Template - "News Article" which is as:

News Article
  Article --(Field section)
    Related Articles --(Multilist - with datasource as '/content/News Listing')

Each article has the other 2 articles as its 'Related Articles'. For eg: Article1 has related articles 2&3 and so on.

Markup:

<h3>Related articles</h3>
<asp:Repeater ID="rpArticles" runat="server" ItemType="Sitecore.Data.Items.Item">
 <HeaderTemplate>
    <ul class="relatedArticles">
 </HeaderTemplate>
 <ItemTemplate>
    <li>
      <sc:Link Field="<%#Item.Paths.FullPath %>" runat="server" Item="<%#Container.DataItem %>">
          <sc:Text Field="Heading" runat="server"/>
      </sc:Link>         
    </li>                                
 </ItemTemplate>
 <FooterTemplate>
    </ul>
 </FooterTemplate>
</asp:Repeater>

Code:

private void Page_Load(object sender, EventArgs e)
 {
   MultilistField relatedArticles = Sitecore.Context.Item.Fields["Related Articles"];
   rpArticles.DataSource = relatedArticles.TargetIDs.Select(id => Sitecore.Context.Database.GetItem(id));
   rpArticles.DataBind();
 }

The above markup is a solution from Here
'Heading' is the name of the field (eg: Article1, Article2 etc..)

When I browse 'News Article 1', the related articles should be 2 & 3, but the output is incorrect & also no anchor tags. Just plain text.

What is wrong in my code.

output:

Related Articles
Article1
Article1

解决方案

Not sure why you changed your original question since it was almost correct. Since the item you are linking to is not specified in a General Link field you cannot use an sc:Link control, instead is far simpler to either use an ASP.Net Hyperlink control which is bound from code behind in the Item_Bound event or simply append the Item URL to an anchor link:

<ItemTemplate>
    <li>
        <a href="<%# Sitecore.Links.LinkManager.GetItemUrl((Sitecore.Data.Items.Item) Container.DataItem) %>">
            <sc:Text Field="Heading" runat="server" Item="<%#Container.DataItem %>"/>
        </a>
    </li>                                
</ItemTemplate>

这篇关于如何链接项目绑定到Sitecore的一个中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆