Sub-site Title as alt tag in image
Question
I have the following code which displays a banner at the top of each page in a sub-site. Each sub-site has a different banner.
For screen readers, I'm trying to get the alt tag to show the title of the SharePoint sub-site. Instead of pulling the title of the sub-site ("Fluffy Bunnies"), it simply renders out the code in the alt tag (<$SPContext.Current.Web.Title>)
<!--SPM: <asp:Image id="subhead" ImageUrl="<% $SPUrl:~site/SiteAssets/Subsite_banner.png %>" Tooltip="<$SPContext.Current.Web.Title >" alt="<$SPContext.Current.Web.Title >" runat="server"/>-->
I have also tried using: $SPContext.Current.Item['Title']
to the same effect.
La solution
alt
is not a property of the ASP.NET Image
class. Thus, the attribute is not being executed on the server-side, the value is being passed down to the <img>
element without being processed. Change alt
to AlternateText
which is the supported Image
class property that will execute your code on the server and pass the resolved text to the rendered alt
attribute.
In addition, normally to bind a server-side value, you need to use <%# %>
instead of just <% %>
-- the $SPUrl:
is an ASP.NET expression that is pre-processed by a SharePoint-defined appsetting. The following should get you what you want:
<!--SPM: <asp:Image id="subhead" ImageUrl="<% $SPUrl:~site/SiteAssets/Subsite_banner.png %>" Tooltip="<%# SPContext.Current.Web.Title %>" AlternateText="<%# SPContext.Current.Web.Title %>" runat="server"/>-->
Depending on the type of page, to ensure that the values get bound properly, you may need to add the following to your page:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
</script>