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

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

  1. Dooman

    Dooman Guest

    Hello,

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

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

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

    Any ideas?

    Thanks!
    Randy DeForest
     
    Dooman, Mar 7, 2007
    #1
    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
    #2
    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
    MVP/ASPInsider
    http://aspnet101.com
    http://aspexpress.com
     
    David Wier, Mar 21, 2007
    #3
  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
    #4
  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
    error:

    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 =
    "ActivityClassTypeId";
    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
    ActivityClassTypesFactory(base.Connection);
    ActivityClassTypesCollection objCollection = objFactory.ShowAll();

    m_ddlActivityClass.DataSource = objCollection;
    m_ddlActivityClass.DataValueField = "ActivityClassTypeId";
    m_ddlActivityClass.DataTextField = "DisplayTitle";
    m_ddlActivityClass.DataBind();

    Thanks for the help!

    Bobby
     
    Guest, Mar 21, 2007
    #5
  6. Dooman

    nasskov

    Joined:
    Oct 8, 2007
    Messages:
    1
    Likes Received:
    0
    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
    #6
  7. Dooman

    rabiniak.piotr

    Joined:
    Mar 2, 2012
    Messages:
    1
    Likes Received:
    0

    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;
    }
    else
    {
    var item = new ListItem(text, value);
    item.Selected = true;
    ddl.Items.Add(item);
    }
    }
     
    rabiniak.piotr, Mar 2, 2012
    #7
    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.