What do I need to do in order to declare a new instance of a RadComboBox programmatically, and to add items to it manually?
4 Answers, 1 is accepted
You can use RadCombox from code behind as any other server control:
- Instantiate a new instance of the RadComboBox class
- Add it to the controlls collection
- Add items to the combobox and subscribe to events
Regards,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
I have a custom CatalogPartChrome for my webpart portal. Rather than displaying a bunch of checkbox items like the standard catalogpartchrome, it displays all of the items in a DropDownList, to easily select webparts, and this works fine for me. I wanted to expand the functionality from a DropDownList to a RadComboBox to add a better look and feel, images, etc...
I should also mention that all of my webpart controls are wrapped in an ajax UpdatePanel (which seems to be behaving well).
The relevant parts of my code are:
public class MyCustomCatalogPartChrome : CatalogPartChrome
{
RadComboBox catalogComboBox;
ScriptManager pageScriptManager;
public MyCustomCatalogPartChrome(MyCustomCatalogZone zone)
: base(zone)
{
// As per the how-to that I was shown in the previous post,
// although I'm not sure if it's implemented correctly in
// this case, as this isn't a normal WebControl
this.Zone.Page.ClientScript.RegisterStartupScript(
typeof(CatalogZone), this.Zone.ID,
"_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;",
true);
if (this.Zone.Page.Form != null)
{
string formOnSubmitAtt =
this.Zone.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) &&
formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Zone.Page.Form.Attributes["onsubmit"] =
"_spFormOnSubmitWrapper();";
}
}
pageScriptManager = ScriptManager.GetCurrent(this.Zone.Page);
}
public override void PerformPreRender()
{
base.PerformPreRender();
catalogComboBox = new RadComboBox();
catalogComboBox.ID = "uxCatalogComboBox";
catalogComboBox.Page = this.Zone.Page;
// Need to register the control with the scriptmanager, or a
// runtime error occurs
pageScriptManager.RegisterScriptControl<RadComboBox>(
catalogComboBox);
catalogComboBox.Items.Add(new RadComboBoxItem("Item #1", "1"));
catalogComboBox.Items.Add(new RadComboBoxItem("Item #2", "2"));
catalogComboBox.Items.Add(new RadComboBoxItem("Item #3", "3"));
catalogComboBox.Items.Add(new RadComboBoxItem("Item #4", "4"));
}
public override void RenderCatalogPart(HtmlTextWriter writer,
CatalogPart catalogPart)
{
// Normally there's a bunch of extra code here to render
// all of the available web parts,
// but it's all removed to simplify this problem
catalogComboBox.RenderControl(writer);
}
}
So, what happens in this case is, everything executes seeminly fine, and there is an empty radcombo box displayed in the my catalog control. Beside the empty list, is the text "select". I can click on the empty list, or click on select, but nothing happens.
When I review the elements created in firebug or ie developer toolbar, everything looks like it's there, it's just not displaying correctly.
style="width: 160px; display: -moz-inline-stack;">
summary="combobox">
<tbody>
readonly="readonly" value="" name="uxCatalogComboBox"/>
position: relative; outline-color: invert;
outline-style: none; outline-width: medium;">select</a>
class="RadComboBoxDropDown RadComboBoxDropDown_Default"
style="display: none;">
list-style-image: none; list-style-position: outside;">
name="uxCatalogComboBox_ClientState"/>
Any ideas?
You need to add the combobox control to the controls collection of your web part. Otherwise RadComboBox will not render its skin CSS file and the JavaScript files.
Regards,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
For the record in case anyone else has this problem, it was a little tricky given how the CatalogPartChrome and CatalogPart works (unless I'm missing something obvious):
I couldn't add it to the control collection of the catalogPart, since the only access to the catalogPart was in the RenderCatalogPart(HtmlTextWriter, CatalogPart) method of the CatalogPartChrome, and you can't modify the collection after PreRender (but there is no way to access the catalogPart in the PreRender method).
I couldn't add it to the control collection of the CatalogPartChrome because it doesn't have a control collection, so in the PerformPreRender() method, I tried adding the RadComboBox to the CatalogZone (CatalogPartChrome.Zone), and it worked fine.
Thanks again,
Kevin