RadComboBox.SelectedIndex always 0

9 posts, 0 answers
  1. chrispo
    chrispo avatar
    10 posts
    Member since:
    Jan 2010

    Posted 15 Mar 2010 Link to this post

    I have a RadComboBox thats populated via a method as follows:
    private void GetFileList(string filter, string mode, RadComboBox rc)
        {
            List<RadComboBoxItem> list = new List<RadComboBoxItem>();
            DirectoryInfo di = new DirectoryInfo(tpbStaticVariables.TestQryFolder);

            FileInfo[] xqFiles = di.GetFiles(filter);
            String href, filename, url;
            foreach (FileInfo xq in xqFiles)
            {
                filename = Path.GetFileNameWithoutExtension(xq.Name);
                href = String.Format("Transform.aspx?q={0}&mode={1}&ras=false", filename, mode);
                url = "<br><a href=" + href + ">" + xq.Name + "</a>";
                RadComboBoxItem rcbi = new RadComboBoxItem(filename, href);
                list.Add(rcbi); 
            }
            rc.DataTextField = "Text";
            rc.DataValueField = "Value";
            rc.DataSource = list;
            rc.DataBind();
            Response.Write(tpbStaticVariables.TestQryFolder);
        }

    The items appear in the dropdown correctly, but when I attach a method to the selectedindexchanged event, the selectedindex is always 0.  Obviously what I want is to get the item that's been selected, and the Value property in particular.

    What am I doing wrong? Thanks for any help.

  2. Marcus
    Marcus avatar
    20 posts
    Member since:
    Jan 2009

    Posted 15 Mar 2010 Link to this post

    Hi There - Keep you eye on the following thread as I believe you are having the same issue...

    http://www.telerik.com/community/forums/aspnet-ajax/combobox/problem-with-radcombox-selected-index-changed-event.aspx#1124861

    All the best,

    Marcus
  3. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 15 Mar 2010 Link to this post

    Hello chrispo,

    I was unable to reproduce this issue in the attached page - please see it.

    Perhaps the issue has something to do with the Text or Values of Items. Can you please provide me with a set of Text/Value pairs with which the issue occurs on your side?

    Sincerely yours,
    Simon
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  4. Marcus
    Marcus avatar
    20 posts
    Member since:
    Jan 2009

    Posted 15 Mar 2010 Link to this post

    One issue for this maybe that the values in the Combo are duplicated.

    See if that helps...

    Marcus
  5. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 15 Mar 2010 Link to this post

    Hello Marcus,

    It is a known 'issue' that having two or more Items with same Values results in the first of these to be selected when one of the other is chosen.

    This is due to the fact that RadComboBox searches for the first Item with the specified Value and selects it (if it exists). The resolution in this case is to set unique Values to all Items.

    Best wishes,
    Simon
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. chrispo
    chrispo avatar
    10 posts
    Member since:
    Jan 2010

    Posted 16 Mar 2010 Link to this post

    Hi,

    The text/value pairs comprise unique combinations that look like this:
    Text ="XfpXQueryModule_General"
    Value = "Transform.aspx?q=XfpXQueryModule_General&mode=0&ras=false"

    There's no duplicated values


  7. chrispo
    chrispo avatar
    10 posts
    Member since:
    Jan 2010

    Posted 16 Mar 2010 Link to this post

    To test, I call this method to populate a radcombobox dynamically at page load

    private void GetJunkRCBI(RadComboBox rc)
        {
            rc.Items.Clear();
            for (int i = 0; i < 10; i++)
            {
                RadComboBoxItem rcb = new RadComboBoxItem("Item" + i.ToString(),i.ToString());
                rc.Items.Add(rcb);
            }
        }

    When I trap the selectedindexchanged event I get interesting results: 

    protected void RadComboBox2_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Label1.Text = e.Text + e.Value.ToString();
        }
    the Text property correctly changes as the selecteditem changes, but the Value is always 0.  There are no duplicate values. 

    Can you suggest some code that overcomes this?


  8. chrispo
    chrispo avatar
    10 posts
    Member since:
    Jan 2010

    Posted 16 Mar 2010 Link to this post

    Fixed it! The combobox population was occurring in the PageLoad event but not inside a if (!IsPostBack) block, so I guess each time the page posted back the value was reset to 0

    Appreciate your help 

    C
  9. Paul
    Paul avatar
    1 posts
    Member since:
    Oct 2012

    Posted 11 Feb 2013 Link to this post

    I had the same issue. SelectedIndex was always -1. I realized that the control's loading was done each time a postback is done.... thanks, this thread helped
Back to Top