Create standards compliant links with EPiServer property
If you are using a strict doctype when developing an EPiServer site which you always should do, then you probably are aware of the validation errors caused by the target attribute on the anchor tag that appears when you are using an EPiServer property if the target is set on the specific page.
public class PropertyPageReferenceCtrl: PropertyPageReferenceControl {
public override void CreateDefaultControls() {
if (PageReference.IsValue(PageLink)) {
HyperLink target = new HyperLink();
target.Text = "Link";
target.NavigateUrl = "#";
IPageSource parent = Parent as IPageSource;
if (parent != null) {
PageData page = parent.GetPage(PageLink);
target.Text = HttpUtility.HtmlEncode((string) page["PageName"]);
target.NavigateUrl = page.LinkURL;
if (page["PageTargetFrame"] != null) {
string targetName = ((PropertyFrame) page.Property["PageTargetFrame"]).FrameName;
if (targetName == "_blank") {
UrlBuilder url = new UrlBuilder(page.LinkURL);
Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.Encoding.Default);
target.Attributes.Add("onclick", "window.open('" + url.Uri + "'); return false;");
}
}
if (target.NavigateUrl == "#") {
target.Attributes.Add("onclick", "return false;");
target.Style.Add("cursor", "default");
}
}
CopyWebAttributes(target);
Controls.Add(target);
}
}
}
We inherit from PropertyPageReferenceControl and change the rendering in the CreateDefaultControls method. In this case the code looks exactly like the original property but with the modifications we wanted if the PageTargetFrame is set to _blank.
Now we need to register our class to handle the rendering for all PropertyPageReference properties on our site. This can be done in your global.asax.cs file in the Application_Start method like this:
protected void Application_Start(Object sender, EventArgs e) {
PropertyControlClassFactory.Instance.RegisterClass(typeof(PropertyPageReference), typeof(PropertyPageReferenceCtrl));
}
There you have it! Your done and ready to create x/html valid links with the EPiServer property.
Originally published at kloojed.ghost.io on August 28, 2008.