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

Need to change the value of DropDownList after SelectedIndex event

2 Answers 699 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Ken
Top achievements
Rank 1
Ken asked on 20 May 2014, 02:20 PM
Hi,

I have some code that allows a user to trigger an action from a choice in a DropDownList.  After this action has completed, I would like to reset the index/value of the DropDownList to the first Item on the list (Index = 0).

I am attempting to trigger the action on the SelectedIndexChanged event, but it will not allow me to change the index within the event.  I can change this value within another event, but I am not sure which one to use...   In the past, using Windows Forms, we have used the SelectionChangeCommitted event to do the reset, aftert the event was kicked off with the SelectedIndexChanged event.  What events/event order do I need to accomplish the above scenario?

Thanks
Laura

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 May 2014, 02:42 PM
Hello Laura,

Thank you for writing.

I think that the RadDropDownList.SelectedIndexChanging event is the appropriate one for your specific case. It is possible to determine which is the new item's index and perform the necessary action. Afterwards, cancel the event in order to keep the selection unchanged. Here is a sample code snippet:
public Form1()
{
    InitializeComponent();
    this.radDropDownList1.SelectedIndex = 0;
    this.radDropDownList1.SelectedIndexChanging += radDropDownList1_SelectedIndexChanging;
}
 
private void radDropDownList1_SelectedIndexChanging(object sender, PositionChangingCancelEventArgs e)
{
    switch (e.Position)
    {
        default:
            //perform action according to the selected item's index
            RadMessageBox.Show(string.Format("Selected item at index {0}", e.Position));
            break;
    }          
    e.Cancel = true;
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
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
Ken
Top achievements
Rank 1
answered on 21 May 2014, 05:51 PM
Hi Desislava,

This is working well.  I would just like to add that during the action I am kicking off, I am referencing the index of the selected drop down.  I realized that I have to reference the e.Position to obtain that value.  This is the statement I am using:
radDropDownList1.Items[e.Position] to represent the list item, then I can get the Text and/or Value as well.

Thank you for your help!

Laura
Tags
DropDownList
Asked by
Ken
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Ken
Top achievements
Rank 1
Share this question
or