Developer FAQ

How to deliver data to Form drop downs from C# functions in Composite C1?

Answer:

In Composite C1, developers can define KeySelector values in a data type via C# code. This can be useful if it is necessary to define values for a  data form's dropdowns.

Let’s consider the following example and see how it can be done.

  1. Add this code as .cs file in the /App_Code folder:
    using System.Collections.Generic;
    
    namespace Examples.DataForms
    {
        public class DropDownStrings
        {
            public static IEnumerable<string> GetStrings()
            {
                List<string> list = new List<string>();
                list.Add("String 1");
                list.Add("String 2");
                list.Add("String 3");
                return list;
            }
    
            public static IEnumerable<string> GetStringsWithParameters(bool IsTrue)
            {
                List<string> list = new List<string>();
                if (IsTrue)
                    list.Add("Parameter = TRUE");
                else
                    list.Add("Parameter = FALSE");
                return list;
            }
    
            public static Dictionary<string, string> GetStringsAsKeyValuePairs()
            {
                Dictionary<string, string> keysValues = new Dictionary<string, string>();
                keysValues.Add("Key 1", "Value 1");
                keysValues.Add("Key 2", "Value 2");
                keysValues.Add("Key 3", "Value 3");
                return keysValues;
            }
        }
    }

    Download the source
  2. Create a global data type that contains 3 string fields: "SimpleString", "ParametrizedString" and "KeyValueString".
  3. Edit the data type's form markup ("Edit from Markup") and use the above code:
    <cms:formdefinition xmlns:cms="http://www.composite.net/ns/management/bindingforms/1.0" xmlns="http://www.composite.net/ns/management/bindingforms/std.ui.controls.lib/1.0" xmlns:f="http://www.composite.net/ns/management/bindingforms/std.function.lib/1.0">
      <cms:bindings>
        <cms:binding name="Id" type="System.Guid" />
        <cms:binding name="SimpleString" type="System.String" />
        <cms:binding name="ParameterizedString" type="System.String" />
        <cms:binding name="KeyValueString" type="System.String" optional="true" />
      </cms:bindings>
      <cms:layout>
        <cms:layout.label>
          <cms:read source="SimpleString" />
        </cms:layout.label>
        <FieldGroup>
          <KeySelector Label="Simple list item field" Help="Populated by a function that returns a List&lt;string&gt;">
            <KeySelector.Selected>
              <cms:bind source="SimpleString" />
            </KeySelector.Selected>
            <KeySelector.Options>
              <f:StaticMethodCall Type="Examples.DataForms.DropDownStrings" Method="GetStrings" />
            </KeySelector.Options>
          </KeySelector>
          <KeySelector Label="Parameterized list item field" Help="Populated by a parameterized function that returns a List&lt;string&gt;">
            <KeySelector.Selected>
              <cms:bind source="ParameterizedString" />
            </KeySelector.Selected>
            <KeySelector.Options>
              <f:StaticMethodCall Type="Examples.DataForms.DropDownStrings" Method="GetStringsWithParameters" Parameters="True" />
            </KeySelector.Options>
          </KeySelector>
          <KeySelector Label="Key value pair field" Help="Populated by a function that returns a Dictionary&lt;string,string&gt;" OptionsKeyField="Key" OptionsLabelField="Value" Required="true">
            <KeySelector.Selected>
              <cms:bind source="KeyValueString" />
            </KeySelector.Selected>
            <KeySelector.Options>
              <f:StaticMethodCall Type="Examples.DataForms.DropDownStrings" Method="GetStringsAsKeyValuePairs" />
            </KeySelector.Options>
          </KeySelector>
        </FieldGroup>
      </cms:layout>
    </cms:formdefinition>