I am having a problem trying to databind to a RadComboBox in the codebehind. I have two RadComboBoxes, and when I select something from the first, I want to update the available list in the second. To do this, I am catching the SelectedIndexChanged event for the first combobox and want to set the DataSource property for the second combobox but when I do the DataBind, the following error appears: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
Here is my sub in the codebehind where I am doing this DataBind. When I step through it, the 'found.Factors' list is populated as expected, but the DataBind still does not work.
protected void FactorTypeList_SelectedIndexChanged(object source, RadComboBoxSelectedIndexChangedEventArgs e)
{
//Identify the Factor Type Column/ComboBox.
GridTemplateColumn FactorTypeColumn = (GridTemplateColumn)CRFGrid.Columns.FindByDataField("FactorType.Name");
RadComboBox FactorTypeComboBox = (RadComboBox)FactorTypeColumn.CurrentColumnEditor.ContainerControl.FindControl("FactorTypeList");
//Identify the Factor Column/ComboBox.
GridTemplateColumn FactorColumn = (GridTemplateColumn)CRFGrid.Columns.FindByDataField("Factor.Name");
RadComboBox FactorComboBox = (RadComboBox)FactorColumn.CurrentColumnEditor.ContainerControl.FindControl("FactorList");
////Check on the currently selected item.
if (FactorTypeComboBox.SelectedIndex == 0)
{
//The User has deselected the item so all Factors should be available.
FactorComboBox.DataSource = FactorDataSource;
FactorComboBox.DataBind();
}
else
{
//Update the Factor ComboBox with the Factors for the selected Factor Type.
SustainmentDataClassesDataContext ctx = new SustainmentDataClassesDataContext();
var FactType = from p in ctx.FactorTypes
where p.Name == FactorTypeComboBox.Text
select p;
if (FactType != null)
{
FactorType found = FactType.First<FactorType>();
FactorComboBox.DataSource = found.Factors;
FactorComboBox.DataBind();
}
}
}
Here is my GridTemplateColumn in the markup:
<%
-- Factor Column--%>
<telerik:GridTemplateColumn
DataField="Factor.Name"
HeaderText="<%$ Resources:Sustain, Factor %>">
<headerstyle width="120" />
<EditItemTemplate>
<telerik:RadComboBox
id="FactorList"
runat="server"
skin="Vista"
allowcustomtext="false"
datasourceid="FactorDataSource"
datavaluefield="Id"
datatextfield="Name"
SelectedValue='<%# Bind("FactorId") %>'
AppendDataBoundItems="True"
AutoPostBack="True"
OnSelectedIndexChanged="FactorList_SelectedIndexChanged"
CausesValidation="false">
<Items>
<telerik:RadComboBoxItem
runat="server"
Text="<%$ Resources:Grid, EmptyListItemText%>"
Value="-1" />
</Items>
</telerik:RadComboBox>
<span style="color: Red">*</span>
<asp:CompareValidator
ID="CompareValidatorFactor"
ValueToCompare="<%$ Resources:Grid, EmptyListItemText%>"
Operator="NotEqual"
ControlToValidate="FactorList"
ErrorMessage="<%$ Resources:Sustain, ValidationText_MandatoryField%>"
runat="server"/>
</asp:CompareValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label
id="lblFact"
runat="server"
Text='<%# Eval("FactorName") %>'>
</asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn >
I am in a bit of a rush to get this done and its really frustrating so any help would be greatly appreciated.
Thanks,
Martin