3 Replies - 9478 Views - Last Post: 22 August 2012 - 06:57 AM Rate Topic: -----

#1 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

selecting the dropdownlist value returning always the first item

Posted 22 August 2012 - 06:13 AM

Good afternoon people have been trying to fill a dropdown that populate using sql command, so far so good, when I click on the dropdown it shows all the items, but when I try to click on an item in the dropdown it always returns the first item in the dropdown .... follows the codes, what i want to do is get the selected value and item from the dropdown and save it on a label for future use.

I appreciate all the support possible,

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    utilizador.Text = Me.Context.User.Identity.Name

     If (Not IsPostBack) Then
    escolhePerfil()

     End If
End Sub



Function escolhePerfil() As Boolean
    Dim connstring As String = "Data Source=10.2.24.17;Persist Security Info=True;User ID=sa;Password=11111;database=22222"
    Dim SQLData As New System.Data.SqlClient.SqlConnection(connstring)

    Dim cmdSelect As New System.Data.SqlClient.SqlCommand("i hide") 'if u need that just tell me 
    SQLData.Open()

    Dim dtrReader As System.Data.SqlClient.SqlDataReader = cmdSelect.ExecuteReader()

    If dtrReader.HasRows Then

    DropDownPerfil.DataValueField = "Description"
    DropDownPerfil.DataSource = dtrReader
    DropDownPerfil.DataBind()


    End If
    SQLData.Close()
Return True
    End Function

   Protected Sub DropDownPerfil_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownPerfil.SelectedIndexChanged
    lbperfil2.Text = DropDownPerfil.SelectedValue
    ' lbnome.Text = DropDownPerfil.SelectedValue
End Sub

this is the .aspx

    <asp:DropDownList ID="DropDownPerfil" runat="server" Height="16px" Width="202px" 
                                    CssClass="DropBorderColor" AutoPostBack="True">
                                </asp:DropDownList>



Is This A Good Question/Topic? 0
  • +

Replies To: selecting the dropdownlist value returning always the first item

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5572
  • View blog
  • Posts: 11,913
  • Joined: 02-June 10

Re: selecting the dropdownlist value returning always the first item

Posted 22 August 2012 - 06:49 AM

34 lbperfil2.Text = DropDownPerfil.SelectedValue

I hate SelectedValue because of odd behavior like this.

Here's what's really happening and why this isn't working as you expect:

You are reacting to the .SelectedIndexChanged event. So at this exact tick of the CPU only the .SelectedIndex has changed. The SelectedValue has not yet caught up, so the SelectedValue is still whatever it was before. This would allow someone to validate the choice the user made and override it before the SelectedValue is updated.

Since you are reacting to the .SelectedIndexChanged event you should only trust the .SelectedIndex property. Anything else is a big assumption and will lead to problems as you have learned. This is a good rule for all events: Only trust the property directly affected by the event.

I always use .SelectedIndex and only means a couple extra lines but it has never let me down.

Remember that a combobox has an .Items collection. So just get the element at the .SelectedValue index


lbperfil2.text = DropDownPerfil.Items[DropDownPerfil.SelectedIndex]
It might be () instead of [] for VB, I'm not sure as I'm a C# guy
Was This Post Helpful? 1
  • +
  • -

#3 richad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-April 12

Re: selecting the dropdownlist value returning always the first item

Posted 22 August 2012 - 06:52 AM

View PosttlhIn`toq, on 22 August 2012 - 06:49 AM, said:

34 lbperfil2.Text = DropDownPerfil.SelectedValue

I hate SelectedValue because of odd behavior like this.

Here's what's really happening and why this isn't working as you expect:

You are reacting to the .SelectedIndexChanged event. So at this exact tick of the CPU only the .SelectedIndex has changed. The SelectedValue has not yet caught up, so the SelectedValue is still whatever it was before. This would allow someone to validate the choice the user made and override it before the SelectedValue is updated.

Since you are reacting to the .SelectedIndexChanged event you should only trust the .SelectedIndex property. Anything else is a big assumption and will lead to problems as you have learned. This is a good rule for all events: Only trust the property directly affected by the event.

I always use .SelectedIndex and only means a couple extra lines but it has never let me down.

Remember that a combobox has an .Items collection. So just get the element at the .SelectedValue index


lbperfil2.text = DropDownPerfil.Items[DropDownPerfil.SelectedIndex]
It might be () instead of [] for VB, I'm not sure as I'm a C# guy



U sir deserve my respect :) Thanks
Was This Post Helpful? 0
  • +
  • -

#4 ToshNeox  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 100
  • Joined: 10-December 11

Re: selecting the dropdownlist value returning always the first item

Posted 22 August 2012 - 06:57 AM

I think your problem here is you can't get the value of which item the user has selected from the combobox? If so, you can get the value of the selected drop down with:

ComboBox1.SelectedItem.ToString()



So if you wanted to set a label's text:

Label1.Text = ComboBox1.SelectedItem.ToString()
Label1.Text = "You have selected: " + ComboBox1.SelectedItem.ToString() 'With text before


Was This Post Helpful? -1
  • +
  • -

Page 1 of 1