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

RadDropDownList SelectedValue not setting

1 Answer 327 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Vijay
Top achievements
Rank 1
Vijay asked on 05 Nov 2013, 06:34 AM
Hi, I'm having some issues with setting SelectedValue on a dropdownlist. 

In my application I have a site selection dropdownlist which helps distinguish different transaction sets. 
When edits are currently being made in any of the screens and the user tries to change the site using the dropdownlist, it asks whether or not the user wants to continue (checkDirtyLabour() below). If the user chooses to NOT continue with the site change (stay on same form) I want to ensure the dropdownlist doesn't change as well - or in fact, in this case, reverts back to the original value.

As such I try to set rddlSite.SelectedValue = Globals.CurrentSite (shared public variable). I have also tried this with the currentGlobalSite. Neither of these things are working presently, when stepping over these lines the assignation doesn't cause any errors yet also doesn't assign the value. ValueMember for rddlSite = "LookupID" of the LookUpRow record. For instance, the default Globals.CurrentSite value is 7, and changing it to 9 via selection, then cancelling the change when prompted does not return the SelectedValue to 7. 

If someone could help me figure out what's going on, I'd love some help. 

The following snippet is in rddlSite_SelectedValueChanged
Dim lu As LookupRow = DirectCast(rddlSite.SelectedItem.DataBoundItem, LookupRow)
If lu.LookupID <> Globals.CurrentSite Then
    Dim currentGlobalSite As Integer = Globals.CurrentSite
    If Globals.CurrentSite > 0 AndAlso checkDirtyLabour() Then 'if dirty and selected to not change, but not when Globals.CurrentSite not set
        rddlSite.SelectedValue = Globals.CurrentSite
        lh.rddlSite.SelectedValue = Globals.CurrentSite
        Return
    End If
    lh.rddlSite.Text = ""
    Globals.CurrentSite = lu.LookupID
    lh.rddlSite.SelectedText = lu.FieldID
    If Globals.CurrentSite > 0 Then refreshLabourOnSiteChange()
End If

Thanks, 
Vijay

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Nov 2013, 02:22 PM
Hello Vijay,

Thank you for contacting Telerik Support.

The appropriate way to prevent changing of the selected value in RadDropDownList is using its SelectedIndexChanging event where you can show a confirmation MessageBox and prevent changing of the value as follows: 
public Form1()
{
    InitializeComponent();
 
    List<MyItem> items = new List<MyItem>();
    for (int i = 0; i < 10; i++)
    {
        items.Add(new MyItem(i,"Name" + i));
    }
    radDropDownList1.DataSource = items;
    radDropDownList1.DisplayMember = "Name";
    radDropDownList1.ValueMember = "ID";
 
    radDropDownList1.SelectedIndexChanging += radDropDownList1_SelectedIndexChanging;
    radDropDownList1.SelectedIndexChanged += radDropDownList1_SelectedIndexChanged;
    radDropDownList1.SelectedValueChanged += radDropDownList1_SelectedValueChanged;
}
 
private void radDropDownList1_SelectedValueChanged(object sender, EventArgs e)
{
}
 
private void radDropDownList1_SelectedIndexChanged(object sender, PositionChangedEventArgs e)
{
}
 
private void radDropDownList1_SelectedIndexChanging(object sender, PositionChangingCancelEventArgs e)
{
    DialogResult result = MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.OKCancel);
    if (result == System.Windows.Forms.DialogResult.Cancel)
    {
        e.Cancel = true; // prevents firing SelectedIndexChanged and SelectedValueChanged events
    }
}
 
public class MyItem
{
    public int ID { get; set; }
 
    public string Name { get; set; }
 
    public MyItem(int iD, string name)
    {
        this.ID = iD;
        this.Name = name;
    }
}

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

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
DropDownList
Asked by
Vijay
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or