The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

Discussion in 'ASP .Net' started by Dooman, Mar 7, 2007.

  1. Dooman

    Dooman Guest


    I am getting this error when binding a drop down list box to a

    The 'SelectedIndex' and 'SelectedValue' attributes are mutually

    I have looked at other posts and they al refer to this error when
    accessing data in the list box.

    Any ideas?

    Randy DeForest
    Dooman, Mar 7, 2007
    1. Advertisements

  2. Dooman

    Guest Guest

    I am having this same problem when filling a drop down list. Did you ever
    find a solution to this? Any help would be appreciated.
    Guest, Mar 21, 2007
    1. Advertisements

  3. Dooman

    David Wier Guest

    What kind of error are you receiving?
    It would be helpful to know exactly what the error says - and then, possibly
    your code for population.

    David Wier
    David Wier, Mar 21, 2007
  4. Dooman

    Mark Rae Guest

    Yes indeed - I've seen this error before when tying to select a ListItem
    programmatically, but never during databinding...

    As has already been mentioned, please post your code...
    Mark Rae, Mar 21, 2007
  5. Dooman

    Guest Guest

    The error is odd, and only happens in one situation with me. I'll post code
    below after I explain. The only time the error happens is when a control
    with the same drop down information and ID on a previous page has a set
    value. For instance, on my listing page I have a filter drop down with id
    m_ddlActivityClass. When I select a filter value from that drop down and
    postback, the list is altered. At that point, I choose an item from the
    filtered list (gridview) to move to another page with detail of that item.
    This page is called through a <a> link and never posts anything back, just
    passes an ID with it for the next page. It is the next page that causes this

    The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information about
    the error and where it originated in the code.

    Exception Details: System.ArgumentException: The 'SelectedIndex' and
    'SelectedValue' attributes are mutually exclusive.

    Source Error:

    Line 224: m_ddlActivityClass.DataValueField =
    Line 225: m_ddlActivityClass.DataTextField = "DisplayTitle";
    Line 226: m_ddlActivityClass.DataBind();
    Line 227: }
    Line 228:

    Again, this only happens when I have selected a value in the drop down on
    the previous page. Otherwise, everything binds just fine. At the point from
    which it crashes, I have not yet tried to set the selected value.

    The code is as follows:

    ActivityClassTypesFactory objFactory = new
    ActivityClassTypesCollection objCollection = objFactory.ShowAll();

    m_ddlActivityClass.DataSource = objCollection;
    m_ddlActivityClass.DataValueField = "ActivityClassTypeId";
    m_ddlActivityClass.DataTextField = "DisplayTitle";

    Thanks for the help!

    Guest, Mar 21, 2007
  6. Dooman


    Oct 8, 2007
    Likes Received:
    I had the same problem, this seems to be a bug in the dropdown list control.
    I lost several hours searching for solution, but didn't found anything, so I had to find a way myself. And here it is:
    Just before the databinding set the .SelectedIndex = -1
    That helps :)
    Last edited: Oct 26, 2007
    nasskov, Oct 8, 2007
  7. Dooman


    Mar 2, 2012
    Likes Received:

    Thank you mate.
    That saved my time :D

    btw: new methods that fixed this problem:

    public static void SetSelectedValue(this ListControl ddl, string value)
    if (ddl.Items.FindByValue(value) != null)
    ddl.SelectedIndex = -1;
    ddl.SelectedValue = value;

    public static void SetSelectedValue(this ListControl ddl, string value, string text)
    if (ddl.Items.FindByValue(value) != null)
    ddl.SelectedIndex = -1;
    ddl.SelectedValue = value;
    var item = new ListItem(text, value);
    item.Selected = true;
    rabiniak.piotr, Mar 2, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.