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

Setting DataSource property leaves an empty Items collection

2 Answers 55 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
IT Department
Top achievements
Rank 1
IT Department asked on 08 Jul 2014, 07:43 PM
I have created a custom control wrapper for the DropDownList. My main reason was to create some shortcut methods for custom objects that we are using to populate the list.

I am trying to write some automated tests for my custom control. The problem is that when I create an instance of the custom control in the automated test (but not actually put it on a form or control), some of the data functionality is lost. I can set the DataSource property (using a list of my custom objects), but the Items collection still shows a count of 0. I can set the SelectedIndex proprty to a valid value, but the SelectedItem property remains null. When I do this same thing with an actual control on a form, the Items collection is populated and reflects what I set in the DataSource property.

 I assume that the DropDownList is borrowing some functionality from the form to tie the DataSource and Items properties together.

Is there something additional that I can do to make the DropDownList fully functional in an automated test, or is this not possible?

I have tried using the Rebind method and this does not help. I also tried not using my custom control and just creating a RadDropDownList object directly in my automated test and it behaves the same way.

2 Answers, 1 is accepted

Sort by
0
Accepted
George
Telerik team
answered on 11 Jul 2014, 01:05 PM
Hello Grant,

Thank you for writing.

The reason why you cannot bind RadDropDownList while not in a form is that it uses the BindingContext of the form to get the CurrencyManager which is used to parse the DataSource. Since all the controls are UI elements their purpose is to be added to a Form or a UserControl. If you want to use the DataSource property in such automated tests you should also create a form and add the DropDownList to the Form:
Form f = new Form();
RadDropDownList list = new RadDropDownList();
f.Controls.Add(list);

Another way is to add items to the DropDownList directly using the ItemsCollection:
RadDropDownList list = new RadDropDownList();
for (int i = 0; i < 10; i++)
{
    list.Items.Add(new RadListDataItem("Text", "value"));
}

I hope this helps.

Regards,
George
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
IT Department
Top achievements
Rank 1
answered on 29 Jul 2014, 03:29 PM
This worked, thank you.
Tags
DropDownList
Asked by
IT Department
Top achievements
Rank 1
Answers by
George
Telerik team
IT Department
Top achievements
Rank 1
Share this question
or