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

RadComboBox.SelectedIndex always 0

8 Answers 855 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
chrispo
Top achievements
Rank 1
chrispo asked on 15 Mar 2010, 01:24 PM
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.

8 Answers, 1 is accepted

Sort by
0
Marcus
Top achievements
Rank 1
answered on 15 Mar 2010, 02:37 PM
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
0
Simon
Telerik team
answered on 15 Mar 2010, 02:46 PM
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.
0
Marcus
Top achievements
Rank 1
answered on 15 Mar 2010, 02:56 PM
One issue for this maybe that the values in the Combo are duplicated.

See if that helps...

Marcus
0
Simon
Telerik team
answered on 15 Mar 2010, 03:11 PM
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.
0
chrispo
Top achievements
Rank 1
answered on 16 Mar 2010, 12:35 PM
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


0
chrispo
Top achievements
Rank 1
answered on 16 Mar 2010, 01:37 PM
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?


0
chrispo
Top achievements
Rank 1
answered on 16 Mar 2010, 02:03 PM
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
0
Paul
Top achievements
Rank 1
answered on 11 Feb 2013, 05:15 PM
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
Tags
ComboBox
Asked by
chrispo
Top achievements
Rank 1
Answers by
Marcus
Top achievements
Rank 1
Simon
Telerik team
chrispo
Top achievements
Rank 1
Paul
Top achievements
Rank 1
Share this question
or