PropertyAdapters updated

by: Anders Hattestad

Have added some cool functionality to Hattis.PageAdapters

If you use EPiServer:Property to show your attribute you can try out these 4 new Attribute tags:

  • Format
  • Remove
  • Text
  • Translate

EPiServer out of the box will render the following:

<EPiServer:Property PropertyName="PageName"  ID="Property1"  DisplayMissingMessage="false" EnableViewState="false" runat="server" />
<EPiServer:Property PropertyName="MainBody"  DisplayMissingMessage="false" EnableViewState="false" runat="server" />

as this html code:

image

I have added some logic in CreateDefaultControls that checks additional attributes to the EPiServer:Property tag

Format

If the control have ITextControl or  Hyperlink the text will be inserted in the format string provided.

<EPiServer:Property Format="<h1>{0}</h1>" PropertyName="PageName"  ID="Property1"  DisplayMissingMessage="false" EnableViewState="false" runat="server" />
<EPiServer:Property PropertyName="MainBody"  DisplayMissingMessage="false" EnableViewState="false" runat="server" />

 

image

Remove

If you add Remove=true then the outer control will be used, and only the text will be displayed.

image

This is some of the same concept Itera.Property uses, but there is no need to change the web control.

Text and Translate

If you add a Text attribute and/or a Translate attribute that value will be {1} and can be used like this

 

<EPiServer:Property 
Text="default text:" Format="<h1>{1}{0}</h1>" Remove="true" PropertyName="PageName" ID="Property1" DisplayMissingMessage="false" EnableViewState="false" runat="server" /> <EPiServer:Property Remove="true" PropertyName="MainBody" DisplayMissingMessage="false" EnableViewState="false" runat="server" /> <EPiServer:Property ID="Property2" PropertyName="PageLink" Format="{1} : {0}" Text="Read more" Translate="/readmore" runat="server" />

Will render like this

image

Dope

These techniques work even with dope, but the extra format tags will not be displayed.

image

The code

the main part of the actully code that does these tricks is something like this.

public override void CreateDefaultControls()
{
    base.CreateDefaultControls();
    AddDisplayHacks();

}
public void AddDisplayHacks()
{
    if (EditControl != null)
    {
        Control use = EditControl;
        if (use is Panel && use.Controls.Count>0)
            use = EditControl.Controls[0];
        if (use is ITextControl)
            (use as ITextControl).Text = GetNewText((use as ITextControl).Text);
        else if (use is HyperLink)
            (use as HyperLink).Text = GetNewText((use as HyperLink).Text);
        if (EditControl is WebControl)
        {
            (EditControl as WebControl).Attributes.Remove("Format");
            (EditControl as WebControl).Attributes.Remove("Text");
            (EditControl as WebControl).Attributes.Remove("Translate");
        }
    }
}
string GetNewText(string text)
{
    string text2 = GetTranslate(GetText(""));
    string format = GetFormat("{0}");
    string result = string.Format(format, text, text2); ;
    if (string.IsNullOrEmpty(text))
        EditControl.Visible = false;
    else
    {
        string removeTag = GetAttribute("Remove", "false");
        if (removeTag.ToLower() == "true")
        {
            Literal l = new Literal();
            l.Text = result;
            this.PropertyDataControl.Controls.Add(l);
            this.PropertyDataControl.Controls[0].Visible = false;
        }
    }
    return result;
}

 

Download

Is available on epicode

or

Direct download here

16 April 2009


Comments

  1. Nice! No need to change from the EPiServer Property control, just change it in-place.
  2. Cool! I guess the same way could be used to set default values to EnableViewState and DisplayErrorMessage.
Post a comment    
User verification Image for user verification  
EPiTrace logger