Tag Archives: episerver cms 5

EPiServer:PageList with ItemDataBound

In my last post I explained how you can extend your episerver:pagelist with your own templates (i.e. SelectedItemTemplate, AlternatingItemTemplate). In this post I will show you how to extend your PageList so you can use ItemDataBound. This is very useful if you want to use asp:controls in your templates and then populate them from code behind.

In this post I will start with the code from my last post and extend it further.

1. Creating PageListEventArgs
First of all we need to create our own EventArgs by extending EventArgs. In this case we need to access to the PageData that is bound to the Template, and we also need access to the Template which should be rendered. So this is the class we need:

public class PageListEventArgs : EventArgs
{
public PageData DataItem { get; set; }
public Control ControlItem { get; set; }
}

If you look at the RepeaterItemEventArgs, which is used by the asp:Repeater, you can get some ideas what else that can be useful in this EventArgs.

2. Creating PageListEventHandler
This is where we create our Eventhandler that will use the PageListEventArgs we just made. It’s just two lines of code:

[Serializable]
public delegate void PageListEventHandler (object sender, PageListEventArgs e);

3. Add PageListEvenHandler to the PageList
Ok, now that we have made an EventHandler we need to use it somehow. So, in the PageList we add the PageListEventHandler:

public event PageListEventHandler ItemDataBound;

By doing this, we can now add events to our PageList. We now need to make sure that the event is triggered when it should. So let’s jump into CreateChildControls, and find where the Item/SelectedItemTemplate is added. Right below the

ItemTemplate.InstantiateIn(control2);

we add

if (ItemDataBound != null)
{
PageListEventArgs args = new PageListEventArgs();
args.ControlItem = control2;
args.DataItem = pages[i];
ItemDataBound.Invoke(this, args);
}

When this is done, we have the option to add the ItemDataBound-event to our PageList!

This is how you do it:

<Antecknat:PageList ID="PageList1" PageLinkProperty="PageLink" OnItemDataBound="PageList1_ItemDataBound" runat="server">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
</ItemTemplate>
</Antecknat:PageList>

or from code behind:

PageList1.ItemDataBound += new Antecknat.Web.WebControls.PageListEventHandler(PageList1_ItemDataBound);

And to modify the controls in the current template:

protected void PageList1_ItemDataBound(object sender, <span>Antecknat.Web.WebControls</span>.PageListEventArgs e)
{

Literal l = e.Item.FindControl("Literal1") as Literal;
if(l!=null)
l.Text = e.DataItem.PageName;
}

Add property to multiple page types

How fun is it when you realize that you have to add a property to 20 different page types?
Been there done that. So today when I realized that I had to do it again I wrote an admin plugin instead.

With this plugin you can create a new property just like you do in EPiServer, and then you select wich PageTypes the property should be saved to.

If a Property with the same name already exists it will leave the old one.

This Plugin has only been tested with EPiServer CMS R2, but I think it’ll work with any CMS5 releases.

If you are interested in a plugin like this to EPiServer 4, then you should check out PageTypeUtil on EPiCode.

Download the plugin

Get LinkURL to another language

I’ve had some problem to link to a page to another language then the current language.
Even though I have a PageData-object with anouther languagebranch, it stills makes a link to the current language.

My own solution, building a url by CurrentPage.StaticLink and adding epslanguage=langID, works but it’s not how I want to do it.

So today I found the EPiServer.UriSupport class. In that class you have EPiServer.UriSupport.AddLanguageSelection(pd.LinkURL, pd.LanguageID), just what I’ve been looking for.

Custom property – DropDown

Made my first customized property today, and because I loved it so much in 4.6, the ice breaker was a DropDown property that takes values from the help-text.

Values in help text can be typed in two ways:
“Shown text:value” or just “value” eg. “value1;Shown text:value2”

[Serializable]
[PageDefinitionTypePlugIn]
class PropertyDropDown : EPiServer.Core.PropertyString
{

public override EPiServer.Core.PropertyData ParseToObject(string value)
{
PropertyDropDown prop = new PropertyDropDown();
prop.String = value;
return prop;
}

public override EPiServer.Core.IPropertyControl CreatePropertyControl()
{
// Create an instance of MyCustomPropertyControl that is used
// for displaying the property.
return new PropertyDropDownControl();
}
}

class PropertyDropDownControl : EPiServer.Web.PropertyControls.PropertyDataControl
{
DropDownList dd;

public PropertyDropDown PropertyDropDown
{
get
{
return PropertyData as PropertyDropDown;

}
}

public override void CreateEditControls()
{
dd = new DropDownList();
this.ApplyControlAttributes(this.dd);
Controls.Add(dd);
this.SetupEditControls();
}

public override void CreateOnPageEditControls()
{
dd = new DropDownList();
this.ApplyControlAttributes(this.dd);
Controls.Add(dd);
this.SetupEditControls();
}

protected override void SetupEditControls()
{
dd.Items.Clear();
string[] values = PropertyDropDown.TranslateDescription().Split(';');
foreach (string value in values)
{
ListItem li = new ListItem();
if (value.Contains(":"))
{
li.Text = value.Split(':')[0];
li.Value = value.Split(':')[1];
}
else
{
li.Text = value;
li.Value = value;
}

li.Selected = li.Value.Equals(PropertyDropDown.Value);
dd.Items.Add(li);
}

if (dd.Items.Count == 0)
dd.Items.Add(new ListItem("No values found", ""));
}

public override void ApplyEditChanges()
{
base.SetValue(this.dd.SelectedValue);
}

public override void ApplyOnPageEditChanges()
{
base.SetValue(this.dd.SelectedValue);
}
}
}