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

Last post 02-12-2009 6:26 PM by faridc. 8 replies.

Sort Posts:

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

    12-03-2007, 5:46 PM

     Hello

    I'm getting this error with a dropdownlist in one of my pages. 

    I'm going to try to explain how I get it...

    I have a page with 2 iframes, in one I choose a record from a grid, pass the value by querystring to the second frame, where I have a dropdownlist databinded to a sqldatasource. Then I set dropdownlist.selectedvalue = request.querystring("ID"). So far, so good...

    The problem is that I have a "Next" and a "Previous" button to navigate through the dopdownlist records. When I hit one of them I get the error The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive. Here's my code of one of the buttons:

     

        Protected Sub btNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btNext.Click
    If Me.ddImage.SelectedIndex + 1 <> Me.ddImage.Items.Count Then
    Me
    .ddImage.SelectedIndex = Me.ddImage.SelectedIndex + 1
    Me.frmVal.Attributes("src") = "../ValScans/ValScanSum.aspx?ID=" & Me.ddImage.SelectedValue
    Me.frmImage.Attributes("src") = "../ValScans/Image.aspx?ID=" & Me.ddImage.SelectedValue
    Me.btPrevious.Enabled = True
    Me
    .btNext.Enabled = True
    Else
    Me
    .btNext.Enabled = False
    End If

    If Me
    .ddImage.SelectedIndex + 1 = Me.ddImage.Items.Count Then
    Me
    .btNext.Enabled = False
    End If
    End Sub

     The strange is that if I run the page without choosing one ID from the other iframe, I don't get the error. And also, when I debug the code, I get exactly the same values from the dropdownlist SelectedIndex and SelectedValue.

    I'm really clueless! I've searched the net for answers, but none answers my problem.

    Thank's a lot

    Paula
     

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-03-2007, 6:00 PM
    • Participant
      1,221 point Participant
    • PureWeen
    • Member since 04-09-2005, 5:44 PM
    • Posts 317

    You might be running into a problem because you are setting the selectedValue and SelectedIndex in the same postback ....
    Exactly what line are you getting the error?
    What you might try doing is clearing the selection before you set the index

    dim index as Integer = Me.ddlImage.SelectedIndex + 1
    Me.ddlImage.ClearSelection()
    Me
    .ddImage.SelectedIndex = index
                Me.frmVal.Attributes("src") = "../ValScans/ValScanSum.aspx?ID=" & Me.ddImage.SelectedValue
                Me.frmImage.Attributes("src") = "../ValScans/Image.aspx?ID=" & Me.ddImage.SelectedValue
                Me.btPrevious.Enabled = True
                Me
    .btNext.Enabled = True


    or if that doesn't work maybe just retrieve the Item Index instead of the selected Value
    Me.ddlImage.Items[ME.ddlImage.SelectedIndex].Value

    If none of that works can you post more of your code? Namely any other places you are setting the selectedvalue/index of the dropdownlist

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-03-2007, 6:29 PM

     Hello PureWeen

    Thank you very much for your answer.

    I tried all your sugestions, but none worked.

    I found the line that's causing the error:

     

                Me.ddImage.SelectedIndex = Me.ddImage.SelectedIndex + 1

     Is there another way to get the previous/next record of the dropdownlist?

    I have code on the previous button, that is the same, except that I set "selectindex - 1".

    And I have code on the SelectedIndexChange, that also has a strange behaviour... The SelectedIndex doesn't change, and It doubles the number of records on the dropdown!!!

     

        Protected Sub ddImage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddImage.SelectedIndexChanged
    Me.frmVal.Attributes("src") = "../ValScans/ValScanSum.aspx?ID=" & Me.ddImage.SelectedValue
    Me.frmImage.Attributes("src") = "../ValScans/Image.aspx?ID=" & Me.ddImage.SelectedValue
    If Me.ddImage.SelectedIndex = 0 Then
    Me
    .frmVal.Attributes("src") = "" Me.frmImage.Attributes("src") = "" End If
    If Me
    .ddImage.SelectedIndex + 1 = Me.ddImage.Items.Count Then
    Me
    .btNext.Enabled = False
    Else
    Me
    .btNext.Enabled = True
    End If
    End Sub

      Am I going crazy, or this doesn't make any sense???

    When I run the page without getting a ID by querystring, it functions normally!

    Thank you

    Paula 

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-03-2007, 6:35 PM
    • Participant
      1,221 point Participant
    • PureWeen
    • Member since 04-09-2005, 5:44 PM
    • Posts 317

    What if anywhere you set the selectedindex you just do it like so

    ddlImage.SelectedValue = ddlImage.Items[SOMENUMBER].Value

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-03-2007, 6:47 PM

     Same error... Crying

    And the ID that I pass by querystring is not used on the sqldatasource. I only use it to set the ddImage.selectedValue = request.querystring("ID")  if a record was choosed on the other frame.

    I don't know if it helps, but here's my Page Load code:

     

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.hfImage.Value = Request.QueryString("ID")

    If Request.QueryString("St") = "1" Then

    Me
    .radBeginDate.SelectedDate = Me.radBeginDate.MinDate
    Me.radEndDate.SelectedDate = Date.Now
    Me.btPrevious.Enabled = False
    Me
    .btNext.Enabled = True
    Me
    .frmVal.Visible = True
    Me
    .frmImage.Visible = True
    Me
    .frmVal.Attributes("src") = "../ValScans/ValScanSum.aspx?ID=" & Me.hfImage.Value
    Me.frmImage.Attributes("src") = "../ValScans/Image.aspx?ID=" & Me.hfImage.Value
    Dim dv As System.Data.DataView = CType(Me.sdsImages.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
    Dim i As Integer = dv.Table.Rows.Count - 1
    If i >= 0 Then
    Dim
    dr As Data.DataRow = dv.Table.Rows(i)
    Dim dr_Begin As Data.DataRow = dv.Table.Rows(0)
    Me.radBeginDate.SelectedDate = Date.Parse(dr_Begin.Item("BeginDate")).ToShortDateString
    Me.radEndDate.SelectedDate = Date.Parse(dr.Item("EndDate")).ToShortDateString
    End If
    Me
    .ddImage.SelectedValue = Me.hfImage.Value
    End If

    If Me
    .ddImage.SelectedIndex = 0 Then
    Me
    .btPrevious.Enabled = False
    Else
    Me
    .btPrevious.Enabled = True
    End If

    End Sub

      Thank you

    Paula 

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-03-2007, 7:15 PM
    Answer
    • Participant
      1,221 point Participant
    • PureWeen
    • Member since 04-09-2005, 5:44 PM
    • Posts 317

    well you are setting the selectedvalue of your dropdown in your page load every time the page loads which isn't good

    do this

    if not Page.IsPostBack
    If
    Request.QueryString("St") = "1" Then
    ........
    End If

    End If

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    12-04-2007, 4:31 PM

     Hi PureWeen

    You're right, the error was caused because of the selectedValue on Page Load.

    Note to myself, have a good night sleep before I post anything!

    Since I couldn't use the page.IsPostBack instruction because that's indicate that a value is sent from the other frame, I replaced it by a session variable and change the value after I use it.

    Thank's a lot for everything!

    Paula 

  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    10-03-2008, 12:55 PM
    • Member
      120 point Member
    • JohnThompson
    • Member since 06-05-2008, 6:42 PM
    • UK
    • Posts 29

    I just had to say something.

    It would appear that there are people all over the net suffering from this error message for want of it being worded better. I had given up and written a routine to search the for the correct index in my DropDownList and use SelectedIndex to make the selection in code, but looking through my code there was a solitary statement using "SelectedIndex"! It is a bit pathetic that you can only use one or the other during a single posting, but that does seem to be the cause of the problem.

    Thanks!

     John

    John Thompson
  • Re: The 'SelectedIndex' and 'SelectedValue' attributes are mutually exclusive

    02-12-2009, 6:26 PM
    • Member
      2 point Member
    • faridc
    • Member since 02-12-2009, 6:24 PM
    • Posts 1

    rbListAcademicDegrees1.SelectedValue = rbListAcademicDegrees1.Items[0].Value; //This hack might resolve your problem if you want to use selectedValue and SelectedIndex together

Page 1 of 1 (9 items)