SmartLabel: My Shiny New Label Control With Smart Tag Support

Want to use Smart Tags in your web apps too? Now you can...

using System;
using System.Web.UI.WebControls;

namespace Activehead.Web.UI
{
 
 public class SmartLabel : Label
 {
  public SmartLabel()
  {
   
  }

  string smartTagNamespace="urn:schemas-microsoft-com:office:smarttags";  
  string smartTagName="PersonName";

  public string SmartTagNamespace
  {
   get
   {
    return smartTagNamespace;
   }
   set
   {
    smartTagNamespace = value;
   }
  }

  public string SmartTagName
  {
   get
   {
    return smartTagName;
   }
   set
   {
    smartTagName=value;
   }
  }

  const string smartTagBehavior = @"<OBJECT id=ieooui classid=clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D></OBJECT><STYLE>odc\:*{behavior:url(#ieooui)}</STYLE>";
  const string smartTagType = @"<o:SmartTagType name="SmartTag" namespaceuri="{0}"></o:SmartTagType>";

  protected override void OnPreRender(EventArgs e)
  {
   base.OnPreRender (e);

   if(!Page.IsClientScriptBlockRegistered("__registerSmartTags"))
   {
    Page.RegisterClientScriptBlock("__registerSmartTags", smartTagBehavior);
   }
   string nsScriptID = "__smartTagNamespace_"+SmartTagNamespace;
   if(!Page.IsClientScriptBlockRegistered(nsScriptID))
   {
    Page.RegisterClientScriptBlock(nsScriptID, String.Format(smartTagType, SmartTagNamespace));
   }
  }

  protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
  {
   writer.Write("", SmartTagName);
   base.RenderContents (writer);
   writer.Write("
", SmartTagName);
  }
 }
}

Make sure that the html header element of your web form looks like this:

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:odc="urn:schemas-microsoft-com:office:smarttags">

And you should be good to go (assuming Office XP is installed on the computer you are browsing with). Probably some things I should add, but this illustrates the point...

No Comments