This is a migrated thread and some comments may be shown as answers.

[Solved] Error on RadComboBox DataBind

1 Answer 411 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 16 Jun 2009, 12:44 PM
Hi there,

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

1 Answer, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 17 Jun 2009, 03:09 PM
Hi Martin,

Try setting the selected item of RadComboBox after rebind by finding the corresponding item value instead of the Bind() expression. You can get to your data item where RadComboBox is initialized using:

GridDataItem item = (sender as RadComboBox).NamingContainer as GridDataItem;


Sincerely yours,
Veli
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.
Tags
Grid
Asked by
Martin
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or