Dynamic Data Custom Field Template – Values List, Take 2 (original) (raw)

An alternative way of doing the Dynamic Data Custom Field Template – Values List this version uses the UIHint’s ControlParameters property to pass in values to the control.

[MetadataType(typeof(Value.Metadata))] partial class Value { internal class Metadata { public object Id { get; set; } [UIHint("ValuesList", null, "ValueList", "Y,N,N/A")] //[ValuesList("Y,N,N/A")] public object TestR { get; set; } [UIHint("ValuesList", null, "ValueList", "Y,N,Other")] //[ValuesList("Y,N,Other")] public object Test { get; set; } } }

Listing 1 – Metadata using the UIHint’s ControlParameters

See the example of using UIHint’s ControlParameters in Listing 1. In the above example you can see that I am passing in the ValueList in the UIHint’s ControlParameters as a key value pair.

///

/// Populates a list control with all the values from a parent table. /// /// The list control to populate. protected new void PopulateListControl(ListControl listControl) { var uiHint = Column.GetAttribute(); if (uiHint == null || uiHint.ControlParameters.Count == 0 || !uiHint.ControlParameters.Keys.Contains("ValueList")) throw new InvalidOperationException(String.Format( "ValuesList for FieldTemplate {0} field is missing from UIHint", Column.Name));

var valuesList = ValuesListAttribute(
    uiHint.ControlParameters["ValueList"].ToString());
if (valuesList.Count() == 0)
    throw new InvalidOperationException(String.Format(
        "ValuesList for FieldTemplate {0} field, is empty",
        Column.Name));

listControl.Items.AddRange(valuesList);

}

Listing 2 – PopulateListControl

In Listing 2 you can see the extraction of the ValuesList from the _UIHint_’s ControlParameters collection.

So Listings 1 & 2 will get you the same as the previous article but without the new attribute.

In take two of this control binh said “But would you change a little to handle to have combobox with value and display value. The Value will be save to database and The Display value will be showed to user.” so I thought I would add this version here.

My personal view is this should be done either via an FK relationship or a Enum value. (Both my Your First FieldTemplate and Dynamic Data Preview 4 have an Enum FieldTemplate) but here goes.

In the Edit FieldTemplate this is all you need to do is modify the ValuesList method.

public ListItem[] GetValuesList(String values) { var items = values.Split(new char[] { ',', ';' }); var listItems = (from item in items select new ListItem() { Text = item.Split(new char[] { '|' })[1], Value = item.Split(new char[] { '|' })[0] }).ToArray(); return listItems; }

Listing 3 – updated ValuesListAttribute

[MetadataType(typeof(Value.Metadata))] partial class Value { internal class Metadata { public object Id { get; set; } [UIHint("ValuesList", null, "ValueList", "1|Y,2|N,3|N/A")] public object TestR { get; set; } [UIHint("ValuesList", null, "ValueList", "1|Y,2|N,3|Other")] public object Test { get; set; } } }

Listing 4 – sample metadata

Here you can see that the values and text are seperated by the pipe symbol ‘|’. However having done this you would now need to create a ValuesList (ReadOnly) FieldTemplate to get the ValuesList and the match the value in the field and then display the correct value, which in my opinion would be quite messy, so here I will stick to using Enums.

Download