Results 1 to 4 of 4

Thread: Getting the selection from dropdownlist

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    9

    Getting the selection from dropdownlist

    I'm trying to get the value of the selection from the databound dropdownlist to be displayed in a textbox. Please help.

    I have set the Autopostback property to true.

    However, the value that is displayed in the textbox is always the first value from the database or rather the selected value is always the first value of the dropdownlist.

    Here is the VB code:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    OleDbDataAdapter1.Fill(DataSet11)
    DropDownList1.DataBind()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    Dim str As String

    str = DropDownList1.SelectedItem.Text

    TextBox4.Text = str

    End Sub
    End Class

    Please Help. Any assistance will be very much appreciated

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Check for page postback in the sub load routine

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If ( Not IsPostBack)
    3.          OleDbDataAdapter1.Fill(DataSet11)
    4.          DropDownList1.DataBind()  
    5.     EndIf
    6. End Sub
    Dont gain the world and lose your soul

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    To expand further on that, the reason you are always getting the first value is because the page load event happens before other events are processed. Which means that you are reloading the combo box with values from the database and the selected index gets changed before the selectedindex event gets to process. So when the event actually fires, you will always get the first one.

    Now, if you wrap code in a if statement that checks to see if the page is being posted back, then you can prevent the combobox resetting the data in itself when the page is posted back. Hence the reason DevGrp posted what he did.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    9
    It's working! Thanx so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width