How to include html inside anchor tag in Sitecore

孤人 提交于 2020-01-16 04:35:50

问题


I have a situation where there is HTML inside an anchor tag. And the author should be able to edit both the anchor (href) & other fields.

aspx:

<asp:Repeater ID="rpEvents" runat="server" ItemType="Sitecore.Data.Items.Item">
 <HeaderTemplate>
  <div class="col-md-3">
 </HeaderTemplate>
 <ItemTemplate>
  <a href="offers/spring.html">
    <sc:Image runat="server" Field="offer image" Item=<%#Container.DataItem%> />
    <h3><sc:Text runat="server" Field="Offer Title" Item=<%#Container.DataItem%> /></h3>
  </a>
 </ItemTemplate>
 <FooterTemplate>
  </div>
 </FooterTemplate>
</asp:Repeater>

I would do this by turning the <a> into <asp:Hyperlink> & assigning its NavigateURL property from code behind, but then the author cannot edit it in experience editor.

How is this done in Sitecore.


回答1:


You can wrap the other HTML elements using the Link field, which will allow all fields to continue to be edited from the Experience Editor:

<ItemTemplate>      
    <sc:Link Field="Offer Link" Item="<%# Container.DataItem %>" runat="server">
        <sc:Image Field="Offer Title" Item="<%# Container.DataItem %>" runat="server" />
        <h3><sc:Text Field="offer image" Item="<%# Container.DataItem %>" runat="server" /></h3>
    </sc:Link>
</ItemTemplate> 

The Link can still be set, the image changed or the separate text field edited:




回答2:


An option is to use the sc:EditFrame inside a repeater, that looks like:

<asp:Repeater runat="server" ID="AccordionRowRepeater">
    <ItemTemplate>
        <my:AccordionRow runat="server" ID="AccordionRowItem" RowItem="<%# Container.DataItem %>" />
    </ItemTemplate>
</asp:Repeater>

And the Row control

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AccordionRow.ascx.cs" Inherits="mynamespace.Controls.AccordionRow" %>
<sc:EditFrame id="EditAccordionItem" runat="server" Buttons="/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Accordion Item">
    <li class="<%= LiClass %>">
    <a class="trigger" href="#"><sc:Text runat="server" ID="ItemTitle"/></a>
    <div class="collapsible">
        <sc:Text runat="server" ID="ItemText" />
    </div>
    </li>
</sc:EditFrame>

And add a Field Editor Button to the Edit Frame Buttons More about this Accordion example see User friendly developing with the Sitecore Experience Editor

Or, I often use this simple solution. It also give you the opportunity to display some help text to the content editor.

 <div runat="server" id="PageditorDiv" Visible="False">
     URl: <sc:Link runat="server" ID="link"/>
 </div>

And in the code Behind.

if (Sitecore.Context.PageMode.IsPageEditor)
{
    PageditorDiv.Visible = true;
}


来源:https://stackoverflow.com/questions/36170844/how-to-include-html-inside-anchor-tag-in-sitecore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!