Can't set SelectedItem in RadComboBox in a User Control Custom Designer
Hi
I Used a RadCombo inside a control designer, and i can populate it and get the selected value. But I can't set the selected item (value) in the RefreshUI event.
I can set the selectedItem in the code-behind, but I need to do it in the Designer.js
my .JS:
refreshUI:
function
()
var
data =
this
._propertyEditor.get_control();
this
.get_ddlEvento().set_value(
data.SelectedId
);
//This Doesn't WORK
,
applyChanges:
function
()
var
controlData =
this
._propertyEditor.get_control();
//guarda eventoId
var
e =
this
.get_ddlEvento();
controlData.SelectedEventoId = e.get_selectedItem().get_value();
//This WORKS
,
// ddl Eventos
get_ddlEvento:
function
()
return
this
._ddlEvento;
,
set_ddlEvento:
function
(value)
this
._ddlEvento = value;
,
Hello Mario,
You first need to get a reference to the combobox item that is to be selected and then call its .select method:
var item = combo.findItemByValue(data.SelectedId); if (item) item.select();
Hi ,
Here an example of a complete solution. I'm selecting projects here:
First, the designer (FormDesigner.ascx)
<%@ Control Language="C#" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.Fields"
TagPrefix="sf" %>
<
sitefinity:FormManager
ID
=
"formManager"
runat
=
"server"
/>
<
div
class
=
"sfContentViews"
>
<
h2
>
Select a project:
</
h2
>
<
telerik:RadComboBox
ID
=
"ProjectSelector"
Skin
=
"Sitefinity"
Width
=
"95%"
CssClass
=
"sfTxt"
runat
=
"server"
>
</
telerik:RadComboBox
>
</
div
>
using
System.Collections.Generic;
using
System.Linq;
using
Telerik.Sitefinity.Web.UI.ControlDesign;
using
Projects.Data;
using
System.Web.UI;
using
System;
using
Telerik.Web.UI;
namespace
SitefinityWebApp.Custom.Projects
public
class
FormDesigner : ControlDesignerBase
protected
override
void
InitializeControls(Telerik.Sitefinity.Web.UI.GenericContainer container)
// load in simple mode
base
.DesignerMode = ControlDesignerModes.Simple;
// Projects
var projects = ProjectsManager.GetManager()
.GetProjects()
.Where(x => x.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
foreach
(var item
in
projects)
var p =
new
RadComboBoxItem();
p.Text = item.Title;
p.Value = item.Id.ToString();
this
.ProjectSelector.Items.Add(p);
this
.ProjectSelector.Items.Add(
new
RadComboBoxItem() Text =
"--No specific project--"
, Value = Guid.Empty.ToString(), Selected =
true
);
this
.ProjectSelector.EmptyMessage =
"--No specific project--"
;
/// <summary>
/// Gets the project selector.
/// </summary>
protected
RadComboBox ProjectSelector
get
return
Container.GetControl<RadComboBox>(
"ProjectSelector"
,
true
);
/// <summary>
/// Gets or sets the path of the external template to be used by the control.
/// </summary>
public
override
string
LayoutTemplatePath
get
return
_layoutTemplatePath;
set
_layoutTemplatePath = value;
/// <summary>
/// Gets or sets the designer script path.
/// </summary>
/// <value>
/// The designer script path.
/// </value>
public
string
DesignerScriptPath
get
return
_scriptPath;
set
_scriptPath = value;
/// <summary>
/// Gets the name of the embedded layout template.
/// </summary>
protected
override
string
LayoutTemplateName
get
return
null
;
public
override
IEnumerable<ScriptDescriptor> GetScriptDescriptors()
var scriptDescriptors =
new
List<ScriptDescriptor>(
base
.GetScriptDescriptors());
var desc = (ScriptControlDescriptor)scriptDescriptors.Last();
// Add the RadComboBox to the descriptor collection
var project =
this
.ProjectSelector;
desc.AddComponentProperty(
"projectControl"
, project.ClientID);
// Return the collection as array
return
scriptDescriptors.ToArray();
/// <summary>
/// Gets a collection of <see cref="T:System.Web.UI.ScriptReference"/> objects that define script resources that the control requires.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerable"/> collection of <see cref="T:System.Web.UI.ScriptReference"/> objects.
/// </returns>
public
override
IEnumerable<ScriptReference> GetScriptReferences()
var scripts =
base
.GetScriptReferences()
as
List<ScriptReference>;
if
(scripts ==
null
)
return
base
.GetScriptReferences();
scripts.Add(
new
ScriptReference(DesignerScriptPath));
return
scripts.ToArray();
private
string
_layoutTemplatePath =
"~/Custom/Donations/DonationFormDesigner.ascx"
;
private
string
_scriptPath =
"~/Custom/Donations/DonationFormDesigner.js"
;
Type.registerNamespace(
"SitefinityWebApp.Custom.Projects.Form"
);
SitefinityWebApp.Custom.Projects.FormDesigner =
function
(element)
// initialize controls
this
._projectControl =
null
;
// base initialization
SitefinityWebApp.Custom.Projects.FormDesigner.initializeBase(
this
, [element]);
SitefinityWebApp.Custom.Projects.FormDesigner.prototype =
initialize:
function
()
SitefinityWebApp.Custom.Projects.FormDesigner.callBaseMethod(
this
,
'initialize'
);
,
dispose:
function
()
SitefinityWebApp.Custom.Projects.FormDesigner.callBaseMethod(
this
,
'dispose'
);
,
//=========================================================================================================
// beging refreshUI
//=========================================================================================================
refreshUI:
function
()
var
data =
this
._propertyEditor.get_control();
// load selected project
var
item =
this
.get_projectControl().findItemByValue(data.ProjectId);
if
(item) item.select();
,
//=========================================================================================================
// beging applyChanges
//=========================================================================================================
applyChanges:
function
()
// save selected page
var
controlData =
this
._propertyEditor.get_control();
// save selected project
if
(
this
.get_projectControl().get_selectedItem() !=
null
)
controlData.ProjectId =
this
.get_projectControl().get_selectedItem().get_value();
,
//=========================================================================================================
// beging properties
//=========================================================================================================
// get and set the project control
get_projectControl:
function
()
return
this
._projectControl;
,
set_projectControl:
function
(value)
this
._projectControl = value;
,
SitefinityWebApp.Custom.Projects.FormDesigner.registerClass(
'SitefinityWebApp.Custom.Projects.FormDesigner'
, Telerik.Sitefinity.Web.UI.ControlDesign.ControlDesignerBase);
if
(
typeof
(Sys) !==
'undefined'
) Sys.Application.notifyScriptLoaded();
Thanks you Lubomir and Daniel
for your answers!
I'd manage to solve this.
Daniel,
Hi,
I think you need to add the RadCombobox to the descriptor collection:
// Add the RadComboBox to the descriptor collection
var project =
this
.ProjectSelector;
desc.AddComponentProperty(
"projectControl"
, project.ClientID);
Hi Ishityaq,
Could you try instead of populating the combo box in the InitializeControls() event to do it in the OnPreRender event? Also I think you forgot to post the source to your CustomDesigner.js file - could you please zip everything and post it somewhere and only post a link here - it will be much more convenient for us.
Kind regards,