Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Display names instead of value with "Multiple choice" View modes: 
User avatar
Member
Member
John-CymbalInteractive - 9/19/2011 5:17:22 PM
   
Display names instead of value with "Multiple choice"
I have been searching the internet as well as any Kentico documentation all day on this one. I am not sure why this hasn't been answered before (or why I can't find it...). Anyway, I have a form where I have a multiple choice displayed in a dropdown. This works as expected and saves to the database.

Now when I try to display the selected value on another page, it shows up as the value portion of my options. I am trying to get the friendly name stored in the database instead. I have begun working on the FormControl needed to display the name correctly but I am not sure what table I need to query or what helper function I need to call in order to get the names. I have already looked at these two links and neither solve my problem effectively.

Link 1
Link 2

What I want is for something like this:

1;foo
2;bar

Where the user selects option 'bar' from the drop down, a transformation which just displays the properties will show 'bar' instead of 2.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 9/20/2011 6:00:50 AM
   
RE:Display names instead of value with "Multiple choice"
Hi,

you could use following API to get information about drop down list options (options is a code name of drop down list field):


DataClassInfo classInfo = DataClassInfoProvider.GetDataClass("cms.news");
FormInfo formInfo = new FormInfo(classInfo.ClassFormDefinition);
FormFieldInfo ffi = formInfo.GetFormField("options");
Hashtable options = ffi.Settings;
string valuesWinTextXML = options["options"].ToString();


In case the option of drop down list are defined as:

1;One
2;Two

the code returns string "<item value=\"1\" text=\"One\" /><item value=\"2\" text=\"Two\" />"

Then you could use for example XMLHelper to parse above xml string.

Alternatively, inside your transformation you can define the switch which will assign text to values.

Example:


<script runat="server">
string GetName(object CodeName) {
if (CodeName != null) {
string Code = CodeName.ToString();
switch (Code) {
case "1": return "One";break;
case "2": return "Two"; break;
}
}
return (string)CodeName;
}
</script>

NumberString:<%# GetName(Eval("options")) %>


Best regards,
Ivana Tomanickova

User avatar
Member
Member
John-CymbalInteractive - 9/20/2011 12:13:17 PM
   
RE:Display names instead of value with "Multiple choice"
Thank you. That is exactly what I need. I saw some code like that in the FieldEditor. The only issue I have is that you have hardcoded the data class as "cms.news".

I want to use the classname of the original table that a form item comes from. Is there any way to get that? Possibly using FormFieldInfo?

Thanks a ton.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 9/21/2011 3:51:57 AM
   
RE:Display names instead of value with "Multiple choice"
Hi,

yes, the classname (for example "cms.news") is available for each rendered item in a transformation. You can get this value using

<%# Eval("classname")%>

You can use the method directly in a code behind/or in a script part:

Eval("classname");

Best regards,
Ivana Tomanickova.

User avatar
Member
Member
John-CymbalInteractive - 9/21/2011 10:45:21 AM
   
RE:Display names instead of value with "Multiple choice"
Eval won't work since the control isn't using databinding (I guess). Maybe I should have explained a bit better on what I was trying to achieve. So I am making a FormEngineUserControl like this:


public partial class P2Proot_CMSFormControls_Viewers_ViewCommaSeparated : FormEngineUserControl
{
private string _value;

public override object Value
{
get
{
return _value;
}
set
{
var val = ValidationHelper.GetString(value, "");

if(!string.IsNullOrEmpty(val))
{
// TODO -- This should not be hardcoded
var classInfo = DataClassInfoProvider.GetDataClass("cms.usersettings");
var formInfo = new FormInfo(classInfo.ClassFormDefinition);
var ffi = formInfo.GetFormField(FieldInfo.Name);
var options = ffi.Settings;
var valuesWinTextXML = options["options"].ToString();
var xml = XElement.Parse("<options>" + valuesWinTextXML + "</options>");

var selectedValues = val.Split(new[] {"|"}, StringSplitOptions.RemoveEmptyEntries);

var items = (from e in xml.Elements("item")
where selectedValues.Contains((string)e.Attribute("value"))
select (string) e.Attribute("text"));
foreach(var i in items)
{
_value += i + ", ";
}
_value = _value.Substring(0, _value.LastIndexOf(","));

}
}
}

protected void Page_Load(object sender, EventArgs e)
{
lblValue.Text = _value;
}
}


This works but like I said I can't have the 'cms.usersettings' hard-coded. I am also wondering why the options are not stored in the settings of the FieldInfo property of my class I made. If I could set it up to work like that then I wouldn't have to query the class for its form.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 9/22/2011 8:34:46 AM
   
RE:Display names instead of value with "Multiple choice"
Hi,

Let's go summarize what we already have:

1. On the Form tab you can see the user friendly text value of dropdown list options. This is a default behavior.

2. Now you need to display the value on another page, but in a database is stored the value (not text) for this field and therefore for example instead of "My friendly text" is displayed "MyFriendlyText". For dropdown lists, etc is stored only value in a database.

3. I assume that this data should be displayed in some transformation, shouldn't it? If so please tell us, where exactly the text part of dropdown list should be displayed.

4. To display user friendly text you can create a custom transformation function, or paste the method into the script directly in a transformation.

You can pass two variables (code name of columns) into the method (classname, yourDropDownList ). For example:

<%# MyCustomMethod(Eval("classname"), Eval(""yourdropdownlist)) %>

and inside the method you can get the text using the FormFieldInfo code.

Thank you in advance for information.

Best regards,
Ivana Tomanickova




User avatar
Member
Member
John-CymbalInteractive - 9/22/2011 11:58:16 AM
   
RE:Display names instead of value with "Multiple choice"
I would really prefer to use a Form control instead of using a static method or inline method.

We are going to upgrade to Kentico 6 soon and I see that I can add properties to the form controls. This would allow me to get this control working correctly.

Thanks for your time and help. I learned a lot from your examples.