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

[Solved] Binding in template: DataSource via DataSourceID

9 Answers 476 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Eliyahu Goldin
Top achievements
Rank 1
Eliyahu Goldin asked on 24 Nov 2008, 08:54 PM
I have a RadComboBox control inside an EditTemplate of a GridView:

                                        <telerik:GridTemplateColumn HeaderText="Phone process" UniqueName="PhoneProcess" Display="false" EditFormColumnIndex="1">  
                                            <EditItemTemplate> 
                                                <div style="width:30ex;margin-right:1ex;">  
                                                    <telerik:RadComboBox runat="server" ID="ddlPhoneProcess"    
                                                        Width="100%" 
                                                        EmptyMessage="No phone processes found"     
                                                        AllowCustomText="false"                                                                  
                                                        DataValueField="Key" 
                                                        DataTextField="Value" 
                                                        AppendDataBoundItems="true" 
                                                        AutoPostBack="true" 
                                                        OnSelectedIndexChanged="ddlPhoneProcess_SelectedIndexChanged" 
                                                        SelectedValue='<%# Bind("PhoneProcessId")%>' 
                                                        > 
                                                        <Items> 
                                                            <telerik:RadComboBoxItem  runat="server" Text="-- Not set --" Value="-1" Selected="true" />                                                              
                                                        </Items> 
                                                    </telerik:RadComboBox> 
                                                </div> 
                                           </EditItemTemplate> 
                                        </telerik:GridTemplateColumn> 
 
In the code-behind I databind the combo like this:
            RadComboBox combo = editItem["PhoneProcess"].FindControl("ddlPhoneProcess") as RadComboBox;  
            Dictionary<int, string> phoneProcesses = DataManagers.Proxy.DmPhone.GetPhoneProcesses();  
            combo.DataSource = phoneProcesses;  
            combo.DataBind();  
 
This produces an InvalidOperationException"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.". The error doesn't make much sense since the GridView is properly bound and other telerik controls, like 

RadNumericTextBox 

 

in other template columns bind with no problem.

If I databind the combo with DataSourceID="xxx" to a SqlDataSource, there is no error.

Could be that databinding via DataSource property somehow messes up the context of the databound control?

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Nov 2008, 06:30 AM
Hi,

Here is KB article which explains on Using RadComboBox as editor in template column of RadGrid. Go through it and see if it helps.

Shinu


0
Eliyahu Goldin
Top achievements
Rank 1
answered on 25 Nov 2008, 01:33 PM
Thanks for the article. The examples for automatic operations use declarative databinding with DataSourceID. This confirms my suspucions that there is something wrong with traditional databinding with DataSource property in this scenario.

The question remains: do I have to re-write the code into using DataSourceID?
0
Eliyahu Goldin
Top achievements
Rank 1
answered on 27 Nov 2008, 06:59 AM
Hey, telerik!

I am still interested in your comments whether there is a problem in databinding with DataSource.
0
Veselin Vasilev
Telerik team
answered on 27 Nov 2008, 02:34 PM
Hello Eliyahu Goldin,

When exactly (in which event) do you databind the combobox?

Try commenting this line:
combo.DataBind();

and let us know how it goes.

Regards,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Eliyahu Goldin
Top achievements
Rank 1
answered on 27 Nov 2008, 03:24 PM
I databins the combo in the grid's ItemDataBound event after checking

if

 

((e.Item is GridEditFormItem) && (e.Item.IsInEditMode))

If I comment out combo.DataBind();, obviously, i don't get the error but I don't get any items in the combo either.

 

0
Veselin Vasilev
Telerik team
answered on 01 Dec 2008, 03:13 PM
Hi Eliyahu Goldin,

Please remove the SelectedValue from the Combo's declaration and set it in the code-behind in ItemDataBound event.

I have attached a sample project which I hope will help you.

Greetings,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Eliyahu Goldin
Top achievements
Rank 1
answered on 01 Dec 2008, 03:39 PM
Yeah, but with this I need to code update manually. Basically, this confirms that there is a problem. I will see what is easier - to update manually or to switch to DataSourceID.
0
JN
Top achievements
Rank 1
answered on 25 Feb 2010, 08:03 PM
I'm having the same issue.  Has there been any updates on this?
0
Simon
Telerik team
answered on 04 Mar 2010, 04:34 PM
Hello JN,

This error appears because the Data-Binding expressions require that the parent container is also data bound. When the RadComboBox postbacks in response to the SelectedIndexChanged event the Grid is recreated from ViewState and no actual data binding occurs.

You can resolve the issue as previously suggested by Mr. Vasilev by removing the Bind expression and set the SelectedValue of the RadComboBox manually in the ItemDataBound Event.

Greetings,
Simon
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
ComboBox
Asked by
Eliyahu Goldin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Eliyahu Goldin
Top achievements
Rank 1
Veselin Vasilev
Telerik team
JN
Top achievements
Rank 1
Simon
Telerik team
Share this question
or