Custom field for Events
Hi,
I want to add a combo box to my event as a custom field.I have created a usercontrol which is bound to database table.
it gets added to my event. but due to that field my event data is not getting store.I i remove that field from event, everything is working fine.
Please provide me some code for doing such thing.
Thanks & Regards,
Hi,
Here's how you can achieve this with our Choice Field:
You can inherit from ChoiceField and modify its Choices collection with the items from your database table. However, you also need to create a Definition and an Element for your custom field. Then, when you register the field, choose the type to be Multiple Choice. Here's how this should look:
The Custom Choice Field class that inherits from ChoiceField
[FieldDefinitionElement(
typeof
(CustomChoiceFieldElement))]
public
class
CustomChoiceField : ChoiceField
public
CustomChoiceField()
:
base
()
public
override
Telerik.Sitefinity.Web.UI.Fields.Enums.RenderChoicesAs RenderChoicesAs
get
return
Telerik.Sitefinity.Web.UI.Fields.Enums.RenderChoicesAs.DropDown;
set
base
.RenderChoicesAs = value;
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
this
.Choices.Clear();
ChoiceItem choiceItem =
new
ChoiceItem();
choiceItem.Value =
"first"
;
ChoiceItem choiceItem2 =
new
ChoiceItem();
choiceItem2.Value =
"second"
;
this
.Choices.Add(choiceItem);
this
.Choices.Add(choiceItem2);
base
.InitializeControls(container);
protected
override
string
ScriptDescriptorType
get
return
typeof
(ChoiceField).FullName;
class
CustomChoiceFieldDefinition : ChoiceFieldDefinition
public
CustomChoiceFieldDefinition() :
base
()
this
.FieldType =
typeof
(CustomChoiceField);
/// <summary>
/// Initializes a new instance of the <see cref="SimpleImageFieldDefinition"/> class.
/// </summary>
/// <param name="element">The configuration element used to persist the control definition.</param>
public
CustomChoiceFieldDefinition(ConfigElement element)
:
base
(element)
this
.FieldType =
typeof
(CustomChoiceField);
class
CustomChoiceFieldElement : ChoiceFieldElement
/// <summary>
/// Initializes a new instance of the <see cref="SimpleImageFieldElement"/> class.
/// </summary>
/// <param name="parent">The parent.</param>
public
CustomChoiceFieldElement(ConfigElement parent)
:
base
(parent)
/// <summary>
/// Gets an instance of the <see cref="SimpleImageFieldDefinition"/> class.
/// </summary>
/// <returns></returns>
public
override
DefinitionBase GetDefinition()
return
new
CustomChoiceFieldDefinition(
this
);
Hi,
This works.
Thanks a lot
is this still the correct way to do this? I attempted to follow this, created a custom field just as shown, and I added it to the Documents type no problem, it even shows all the custom items.
However, when I publish, I get this error:
500 (There was an error deserializing the object of type Telerik.Sitefinity.Web.Services.ContentItemContext`1[[Telerik.Sitefinity.Libraries.Model.Document, Telerik.Sitefinity.Model, Version=6.1.4700.0, Culture=neutral, PublicKeyToken=b28c218413bdf563]]. End element 'MyChoices' from namespace '' expected. Found element 'item' from namespace ''.)
What am I missing?
Did you ever get an answer on this? I am trying to add a custom field for BlogPosts that exhibits the same issue when I try to publish. The Thunder generated control works just find as a custom field for News though.