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

[Solved] Binding to RadComboBox at runtime not working

17 Answers 1040 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 12 Nov 2008, 02:16 PM
Hi. I have followed the examples in both the offline documentation and your online demos and have been unable to bind a DataTable to the RadComboBox at runtime. The code appears to execute without a problem, but the dropdown menu for the combo is empty.

I am using the following code with simple, hardcoded values:
        protected void Lookup_ItemsRequested(object sender, RADHeaderTemplate.LookupBoxExpandedArgs e) 
        { 
            DataTable table; 
            RadComboBox combo; 
            DataRow row; 
 
            table = new DataTable(); 
 
            table.Columns.Add("Col1"); 
            table.Columns.Add("Col2"); 
            table.Columns.Add("Col3"); 
 
            row = table.NewRow(); 
            row.ItemArray = new string[] { "Row1Cell1""Row1Cell2""Row1Cell3" }; 
            table.Rows.Add(row); 
 
            row = table.NewRow(); 
            row.ItemArray = new string[] { "Row2Cell1""Row2Cell2""Row2Cell3" }; 
            table.Rows.Add(row); 
 
            combo = e.ComboBox; 
 
            combo.DataSource = table; 
            combo.DataBind(); 
        }     

Is there any reason that nothing shows up in the RadComboBox once this function is complete?

17 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 13 Nov 2008, 07:11 AM
Hi Andrew,

You need to set the "EnableLoadOnDemand" property to True and also include ItemTemplate field for RadComboBox to achieve this functionality. You can try the following approach.

ASPX:
 <telerik:RadComboBox ID="RadComboBox1" Runat="server" EnableLoadOnDemand="True"  
        Height="46px" onitemsrequested="RadComboBox1_ItemsRequested" Width="226px"  
        HighlightTemplatedItems="True" onitemdatabound="RadComboBox1_ItemDataBound"
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
 
                        <ItemTemplate>                             
                              <%# DataBinder.Eval(Container.DataItem, "Col1") %> 
                               
                              <%# DataBinder.Eval(Container.DataItem, "Col2") %> 
                               
                              <%# DataBinder.Eval(Container.DataItem, "Col3") %>                            
                        </ItemTemplate> 
    </telerik:RadComboBox> 

C#:
protected void RadComboBox1_ItemsRequested(object o, Telerik.Web.UI.RadComboBoxItemsRequestedEventArgs e) 
    { 
        DataTable table = new DataTable(); 
        DataRow row; 
        table.Columns.Add("Col1"); 
        table.Columns.Add("Col2"); 
        table.Columns.Add("Col3"); 
        row = table.NewRow(); 
        row.ItemArray = new string[] { "Row1Cell1""Row1Cell2""Row1Cell3" }; 
        table.Rows.Add(row); 
        row = table.NewRow(); 
        row.ItemArray = new string[] { "Row2Cell1""Row2Cell2""Row2Cell3" }; 
        table.Rows.Add(row); 
        RadComboBox1.DataSource = table; 
        RadComboBox1.DataBind(); 
    } 
    protected void RadComboBox1_ItemDataBound(object sender, RadComboBoxItemEventArgs e) 
    { 
        e.Item.Text = ((DataRowView)e.Item.DataItem)["Col1"].ToString() + "  " + ((DataRowView)e.Item.DataItem)["Col2"].ToString() + "  " + ((DataRowView)e.Item.DataItem)["Col3"].ToString(); 
    } 


Thanks and Regards,
Shinu.
0
Andrew
Top achievements
Rank 1
answered on 13 Nov 2008, 12:57 PM
How do I accomplish the same thing without creating the RadComboBox at design time? All my combos are created at runtime based on a configuration file, so I do not know ahead of time how many combos I'm going to end up with nor what data they will contain; not the number of columns, column names nor the list of values.
0
Andrew
Top achievements
Rank 1
answered on 17 Nov 2008, 01:53 PM
Was there a runtime solution for this problem or am I out of luck?

Thanks.
0
Veselin Vasilev
Telerik team
answered on 17 Nov 2008, 02:36 PM
Hello Andrew,

You can check the Adding Templates at Runtime section in our help article.

Kind regards,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew
Top achievements
Rank 1
answered on 17 Nov 2008, 02:51 PM
Creating the RadComobBox in cells in a grid at runtime is not my problem. I've already got that working. Binding a DataTable to a RadComboBox, a RadComboBox that was created via an ItemTemplate at runtime, is what I need to do.

How do I emulate the functionality laid out in Shinu's previous post:

<
%# DataBinder.Eval(Container.DataItem, "Col1") %>

for a RadComboBox at runtime?
0
Andrew
Top achievements
Rank 1
answered on 19 Nov 2008, 04:53 PM
So, I'm able to get the HeaderTemplate displaying the columns I want, similar to the example here (the second RadComboBox) http://demos.telerik.com/aspnet/prometheus/ComboBox/Examples/Default/DefaultCS.aspx

I still cannot, however, figure out how to populate a ROW of items in a RadComobBox.Items collection. Binding a DataTable will only ever put one value in each subsequent row of the RadComboBox's dropdown list.

How do I, at runtime, generate rows, rows that are variable in length and value, from a DataTable into the Items collection for a combobox that is also created at runtime?

This is last thing I need to determine how to do. If I determine that this functionality is indeed possible with this control then I'll have everything I need from the toolkit and will be able to convince my boss that this is the product we need to purchase. So, an example on how to do this would be greatly appreciated.
0
Veselin Vasilev
Telerik team
answered on 20 Nov 2008, 01:33 PM
Hello Andrew,

Please find attached a sample project which demonstrates the solution.

I hope this helps.

Regards,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew
Top achievements
Rank 1
answered on 26 Nov 2008, 04:44 PM
This works perfectly, thank you.

Another couple questions though. I notice that the text value for the combo box is set for each item when that item is data bound. Now, let's say I select an item from the dropdown list, but need one of those values to update the combobox.Text property in another cell on the grid. Is there a simple way to do this; keeping, in mind, of course, that all these combo boxes are generated at runtime and can be in any given position with varying IDs (usually something like "GridComboBox-" + column + "-" + row).

Secondly, I've seen your examples on how to pass values from another combobox back to the server if they are created at design time (using the args.get_context() collection), but have, after much fiddling, not figured out a way to pass that data back if the combos are created at runtime (I don't know their names so I can't create the javascript function at runtime.). I thought it might be posible to physically build the javascript function at runtime and write it out after all the controls in the grid had been created, but it appears that using the ClientID of the objects does not work as the controls in cells of the grid are not written out the way a typical control would be written to the page. Do you see my problem?

Please don't hesitate to ask for clafification if parts of my question don't make sense.

Thank you.
0
Veselin Vasilev
Telerik team
answered on 01 Dec 2008, 10:17 AM
Hello Andrew,

I think the best way to proceed is to send us a sample project demonstrating the problem. We will try to find a solution for you.

Thanks

All the best,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew
Top achievements
Rank 1
answered on 01 Dec 2008, 01:42 PM
I will create and upload a sample project as soon as possible.

Thank you.
0
Andrew
Top achievements
Rank 1
answered on 01 Dec 2008, 04:21 PM
Alright, I've created a sample project that can be downloaded here:

http://dl.getdropbox.com/u/109257/RadGridSample.zip

In the source folder you will see a file entitled "TestScenario.txt." It contains information necessary as to what is being done in the sample and what we need it to do. If you have any questions at all please feel free to ask.

Thank you,
Andrew
0
Veselin Vasilev
Telerik team
answered on 04 Dec 2008, 02:58 PM
Hello Andrew,

Here are some guidelines:

Even if I uncommented the WriteJavascript() method - the script was not rendered on the page.

I removed this line:

ClientScriptManager csm = Page.ClientScript; 

and changed this one:

RadScriptManager.RegisterStartupScript(Page, typeof(Page), "gridCellValues", jScript, true); 

Thus the javascript was rendered on the page and you can use the context object to pass the information to the ItemsRequested event handler.

You can also subscribe to the OnClientSelectedIndexChanged event of the combobox, find the next combobox and set its text. To find the next combobox you can use for example the
Telerik.Web.UI.RadComboBox.ComboBoxes array.

Let us know if you have additional questions.

Sincerely yours,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew
Top achievements
Rank 1
answered on 08 Dec 2008, 01:18 PM
Alright, I'm getting very close to being done now, thank you. A couple of final questions.

1. I'd like to iterate through the Telerik.Web.UI.RadComboBox.ComboBoxes collection in a javascript function. How do I get the count of objects in the collection. This information does not appear to be in the documentation. I've tried Telerik.Web.UI.RadComboBox.ComboBoxes.Length,Telerik.Web.UI.RadComboBox.ComboBoxes.Count, Telerik.Web.UI.RadComboBox.ComboBoxes.get_length() and Telerik.Web.UI.RadComboBox.ComboBoxes.get_count(). None of those appear to work.

2. Similarly to the Telerik.Web.UI.RadComboBox.ComboBoxes collection is there a way to get the RadTextBoxes on a page? I've tried Telerik.Web.UI.RadComboBox.TextBoxes but that does not appear to be a valid object.

Thanks again.
0
Veselin Vasilev
Telerik team
answered on 08 Dec 2008, 01:53 PM
Hello Andrew,

Onto the questions:

1. You should have tried with length (lowercase):

var combos = Telerik.Web.UI.RadComboBox.ComboBoxes;  
for (var i = 0; i < combos.length; i++)  
{  
    alert(combos[i].get_id());  


2. No, there is no such method, but here is one that you can use:

//returns an array of all RadTextBoxes on the page 
function RadTextBoxes() 
    var allRadTextBoxes = []; 
    var allRadControls = $telerik.radControls; 
     
    for (var i = 0; i < allRadControls.length; i++) 
    { 
        var element = allRadControls[i]; 
        var isRadTextBox = element instanceof Telerik.Web.UI.RadTextBox; 
        if (isRadTextBox) 
        { 
            Array.add(allRadTextBoxes, element); 
        } 
    } 
    return allRadTextBoxes; 

I hope this helps.

Greetings,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew
Top achievements
Rank 1
answered on 08 Dec 2008, 02:16 PM
Thanks. Does the RadTextBox have any client-side property accessors? RadTextBox.get_text() and RadTextBox.get_id() do not appear to work like they do for the combo boxes.

Thanks.
0
Veselin Vasilev
Telerik team
answered on 08 Dec 2008, 02:36 PM
Hi Andrew,

You can find the client-side API of the RadTextBox in this help article:
RadTextBox Client Object

I suggest that we keep this thread combobox related only.

Thanks

Greetings,
Veselin Vasilev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
chandra
Top achievements
Rank 1
answered on 04 Apr 2009, 12:14 PM
Thaanks a lot i struggled a lot to solve this problem its working for me    
Tags
ComboBox
Asked by
Andrew
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Andrew
Top achievements
Rank 1
Veselin Vasilev
Telerik team
chandra
Top achievements
Rank 1
Share this question
or