Try our conversational search powered by Generative AI!

Ruslan Minasian
Sep 17, 2010
  6037
(0 votes)

Use UserControl for Editing Custom Property Types

First of all, read this article by Ted Nyberg about using ETF. When I first started using EpiServer (not very long ago) I was wondering why can’t I find any examples of using UserControls in custom properties. I’m pretty sure I’m inventing my own wheel but hopefully my note will be better indexed than existing ones (I found none of them).

So, let’s create property MyPropertyString extending PropertyString.

[Serializable]
[PageDefinitionTypePlugIn]
public class MyPropertyString : PropertyString {
}

Looks like pretty useful property. Let’s add it to some PageType and ensure it’s displayed.

Then, we’ll add new Web User Control

Layout
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyPropertyStringControl.ascx.cs" Inherits="EPiServer1.MyPropertyStringControl" %>
<asp:TextBox runat="server" ID="txtValue"  />&nbsp;
<input type="button" value="Clear"
onclick="document.getElementById('<%= txtValue.ClientID %>').value = ''; return false;" />

 

Code-behind
public partial class MyPropertyStringControl : UserControlBase, IPropertyControl {
    public MyPropertyStringControl() {
        Enabled = true; // Do not forget this!
    }

    public void SetupControl() {
        txtValue.Text = (string)PropertyData.Value;
    }

    public void ApplyChanges() {
        PropertyData.Value = txtValue.Text;
    }

    public bool DisplayEditUI {
        get { return PropertyData.DisplayEditUI; }
    }

    public PropertyData PropertyData { get; set; }

    public PropertyDataCollection Properties { get; set; }

    public RenderType RenderType { get; set; }

    public TableRowLayout RowLayout {
        get { return TableRowLayout.Default; }
    }

    public string ValidationGroup { get; set; }

    public bool Enabled { get; set; }
}

 

And the last line which this post is about:

[Serializable]
[PageDefinitionTypePlugIn]
public class MyPropertyString : PropertyString {
    public override IPropertyControl CreatePropertyControl() {
        return (IPropertyControl)BuildManager.CreateInstanceFromVirtualPath("~/MyPropertyStringControl.ascx", typeof(MyPropertyStringControl));
    }
}

Voila – we’ve got ‘clear’ button near our textbox.

 

P.S. Guys, is there any good code-pasting plug-in for Live Writer?

Sep 17, 2010

Comments

Anders Hattestad
Anders Hattestad Sep 21, 2010 10:33 AM

I have done something simular like this
http://labs.episerver.com/en/Blogs/Anders-Hattestad/Dates/2008/10/User-control-as-a-Property/

Kenia Gonzalez
Kenia Gonzalez Jul 4, 2012 02:33 PM

Great article. Really helpful, thanks!

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog