0

I'm working in a webforms project and I'm trying to add fields that are editable in Experience Editor but they're not showing up on the page. I'm basing this off of other pages that currently implement this and I can't figure out what I'm missing.

aspx:

<%@ control language="C#" autoeventwireup="true" codebehind="Promo.ascx.cs" inherits="MyProject.Promo" %>
<%@ Register Assembly="Sitecore.Kernel" Namespace="Sitecore.Web.UI.WebControls" TagPrefix="sc" %>

<div class="dekalb-promo">
    <div class="wrapper" runat="server" id="divPromo">
        <span class="title">
            <asp:Literal runat="server" ID="litTitle"></asp:Literal>
            <sc:Image ID="imgTitleImage" Field="Title Image" runat="server" />
            <sc:Text ID="txtTitle" Field="Title" runat="server" />
        </span>
        <span class="body">
            <sc:Text ID="txtBody" Field="Description" runat="server" />

        </span>
        <span class="lrn-more">
            <sc:Link ID="linkLrnMore" Field="Link" runat="server" />
        </span>
    </div>
</div>

aspx.cs:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Glass.Mapper.Sc;
using Glass.Mapper.Sc.Web.Ui;
using Sitecore.Diagnostics;
using WebControl = Sitecore.Web.UI.WebControl;
using Sitecore.Globalization;

namespace MyProject
{
    public partial class Promo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var item = ContentHelper.GetContextItem(((WebControl)Parent).DataSource);
            IDekalb_Promo datasource = item.GlassCast<IDekalb_Promo>();
            if (datasource == null) return;

            divPromo.Attributes[Consts.HTML_ATTRIBUTE_STYLE] = "background-image: url('" + datasource.Background_Image?.Src + "')";
            if (datasource.Title_Image != null && !String.IsNullOrEmpty(datasource.Title_Image.Src))
            {
                litTitle.Text = "<img class='title-img' src='" + datasource.Title_Image.Src + "'></img>";
            }

            if (!Sitecore.Context.PageMode.IsExperienceEditor)
            {
                imgTitleImage.Visible = false;
            }

            if (datasource.Link == null || String.IsNullOrEmpty(datasource.Link.Url))
            {
                linkLrnMore.Visible = false;
            }
        }
    }
}
1

The Sitecore controls are looking for the fields on the context item. Since you're using a data source you need to pass that item into each of the controls using the Item property on the control.

<sc:Image ID="imgTitleImage" Field="Title Image" runat="server" Item="DataSourceItemHere" />
2
  • Ah I see, is there a way to globally set this for all Sitecore controls rather than setting each one individually? Oct 1 '19 at 13:22
  • Not that I'm aware of out of the box. You might be able to build something but it is probably clearer to add the property to the controls so future you aren't wondering why it isn't loading off the context item. Oct 1 '19 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.