tags:

views:

51

answers:

4

i have only one element in drop downlist so selection index change is not working... the datasource is given to run time... if it was on designing time i can give the select one list item... but at run time what should i do????????

A: 

In your code when filling the list, check how many items there are, and if it is only one, manually call the method that the selected index changed event is hooked up to.

ck
i dont want to manually call the function
Sheetal Inani
If there is only one option and it is preselected, then it can make the user experience much more streamlined if the next action has already happened.
ck
A: 

Do as below check for the count of items and set the selectedindex property.

if (dcreportingto.Items.Count > 0)
{
     dcreportingto.SelectedIndex = 0;
     //call selected index change 
}
Pranay Rana
i want to call the selected index change method... but there is only one item in the list... so method is not getting called
Sheetal Inani
@Sheetal - you can call selected index change event as suggested in answer
Pranay Rana
+1  A: 

if i've understood correctly your problem you can you add an empty item at design time , set the append databound item to true and selected property of the empty item to true

Stefano
i have to fill the dropdownlist on every submit... so by append data bound.. the same name is coming again and again
Sheetal Inani
yes, i should have warned you,if you refresh the items collection every time you must clear it when you start to rebuild, add the empty item and set its selected property to true. If i remember correctly , you must also set the autopostback to true if you want to see the selectedindexchange on server side. And beware also of ajax components if you have put it into an update panel with some triggering conditions
Stefano
ya you are right
Sheetal Inani
how to add an empty item runtime?????????????
Sheetal Inani
how to clear dropdownlist
Sheetal Inani
comboBox1.Items.Clear();
yonan2236
i think this post answer your question : http://stackoverflow.com/questions/267064/asp-net-add-blank-item-at-top-of-dropdownlist
Stefano
comboBox1.Items.Add("Seek first before you ask.");
yonan2236
what else...? :)
yonan2236
hey thax... its working....
Sheetal Inani
A: 

You want to call SelectedIndexChanged Event? I'm right?

If so, just do this:

private void callSelectedIndex()
{
    ComboBox cb = new ComboBox(); // Some ComboBox control to pass.
    comboBox1_SelectedIndexChanged(cb, null); // Your ComboBox's SelectedIndexChanged event method.
}
yonan2236