RadDropDownList SelectedValue not setting

2 posts, 0 answers
  1. Vijay
    Vijay avatar
    7 posts
    Member since:
    May 2013

    Posted 05 Nov 2013 Link to this post

    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
  2. Dess
    Admin
    Dess avatar
    1607 posts

    Posted 07 Nov 2013 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top