Content type user controls

If the built-in properties or controls does not meet your requirements, you can create your own UI component. This is done by creating a custom user control. A user control is an .ascx file with the possibillity to add existing server controls and markup, define properties and methods for the control. You can embed them in a lemoon content type as a unit. This lets you create any type of control.

Example

The following example adds a listbox control to a content type. This example only outputs some countries, but since you have full control over the output, you could fetch data from a database, use the lemoon api to list pages, use a web service to collect information, etc.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CountrySelector.ascx.cs" Inherits="MyProject.Lemoon.Controls.CountrySelector" %>
<div class="field">
    <label>Country</label>
    <asp:DropDownList id="countrylist" runat="server">
        <asp:ListItem Value="AW">Aruba</asp:ListItem>
        <asp:ListItem Value="AU">Australia</asp:ListItem>
        <asp:ListItem Value="AT">Austria</asp:ListItem>
    </asp:DropDownList>
</div>

Code 1. CountrySelector.ascx

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mindroute.Lemoon.Web.UI;
using System.ComponentModel;
 
namespace MyProject.Lemoon.Controls {
    public partial class CountrySelector : UserControlFormField<string> {
        protected void Page_Load(object sender, EventArgs e) {
            base.CreateChildControls();
        }
 
        public override string Value {
            get {
                EnsureChildControls();
                return countrylist.SelectedValue;
            }
            set {
                EnsureChildControls();
                countrylist.SelectedValue = value;
            }
        }
 
    }
}

Code 2. Test.ascx.cs

Notice that the control inherits from the UserControlFormField

 

Explanation of the code

 

public override string Value {
    get {
        EnsureChildControls();
        return countrylist.SelectedValue;
    }
    set {
        EnsureChildControls();
        countrylist.SelectedValue = value;
    }
}

Code 3. The Value property

 

How to add the user control to your content type

Add the following property to your content type.

[Persisted]
[DataType("~/lemoon/controls/countryselector.ascx")]
public string Country {
    get;
    set;
}

Code 4. The content type class

The DataType attribute specifies the path to the user control.