Image Attributes

Image Attributes

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.ImageField
  • Sitecore.Data.Items.Item
  • Sitecore.MainUtil

Image Attributes C# Web Control Code

//get Simple Fields metadata item from context database using item guid 
Sitecore.Data.Database db = Sitecore.Context.Database; 
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}"); 
if (item == null) return; 

Sitecore.Data.Fields.ImageField imgFld = item.Fields["example image"]; 
if (imgFld == null) return; 

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div); 
output.Write("C# Web Control Real-time Output"); 
output.RenderEndTag(); 

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:small; width:100%; clear:both"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div); 
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold"); 
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P); 
//using : for colon and   for space 
output.Write("{0}: {1}", "Field", imgFld.InnerField.DisplayName); 
output.RenderEndTag(); //P 

output.Write("Alt Text: " + "<b>" + imgFld.Alt + "</b><br/>"); 
output.Write("Border: " + "<b>" + imgFld.Border + "</b><br/>"); 
output.Write("Class: " + "<b>" + imgFld.Class + "</b><br/>"); 
output.Write("Height: " + "<b>" + imgFld.Height + "</b><br/>"); 
output.Write("HSpace: " + "<b>" + imgFld.HSpace + "</b><br/>"); 
output.Write("Is Internal? " + "<b>" + imgFld.IsInternal.ToString() + "</b><br/>"); 
output.Write("Link Type: " + "<b>" + imgFld.LinkType + "</b><br/>"); 
output.Write("Name: " + "<b>" + imgFld.MediaDatabase.Name + "</b><br/>"); 
output.Write("Media ID: " + "<b>" + imgFld.MediaID.Guid.ToString() + "</b><br/>"); 
output.Write("Media Item: " + "<b>" + imgFld.MediaItem.Name + "</b><br/>"); 
output.Write("Media Language: " + "<b>" + imgFld.MediaLanguage.Name + "</b><br/>"); 
output.Write("Media Version: " + "<b>" + imgFld.MediaVersion.Number.ToString() + "</b><br/>"); 
output.Write("Root: " + "<b>" + imgFld.Root + "</b>"); 

output.RenderEndTag(); //Div


Image Attributes C# Web Control Output

Alt Text: Code Snippet
Border: 
Class: 
Height: 16
HSpace: 
Is Internal? True
Link Type: 
Name: web
Media ID: ed5e7c35-df5e-4c19-ba09-c0778a2d23b6
Media Item: code_colored
Media Language: en
Media Version: 0
Root: image