Hello,

I have got a dropdown box that is displaying data from a table in my database.
when i select an item in the list i want to change the text in a label to what has been selected in the list.

this is the code I am using to tie the dropdown list (lstNames) to my database

        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = ../SR.mdb"

        sql = "Select * from SRuser"

        DSeS = ESDatCon.SelectData(sql, con)

        lstNames.DataSource = DSeS
        lstNames.DataValueField = "Uname"
        lstNames.DataTextField = "Uname"

        lstNames.DataBind()


End Sub

I have got this code behind the Protected Sub lstNames_SelectedIndexChanged

lblDisp.Text = lslNames.SelectedValue

However when i change the item in the list the text of the label doen't change

i have also tried

lblDisp.Text = lslNames.SelectedItem

But again nothing happens???

Anyone have any idea how to do this??

Thanks

Wrap your data binding code in Page_Load event with IsPostBack property block.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  IF  If Not IsPostBack Then
     'Code to populate the DropDownList
  End If       
End Sub   

Set DropDownList.AutoPostBack=True and handle the SelectedIndexChanged event.

Protected Sub lslNames_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lslNames
   lblDisp.Text = lslNames.SelectedValue
End Sub

EDIT:

hello,

thanks again for your help!
is there any way to set the drop down list so it doesn't display the the first item?
i.e it appears blank or has default text like please select?

it's just that i'm finding that to select the first item i have to select the second item then the first so it puts the value in my label!

Thanks!

iain

Yes! You may insert an empty entry after binding the DropDownList:

lstNames.DataTextField = "Uname"
lstNames.DataBind()
lstNames.Items.Insert(0, New ListItem("***Select***", "0"))

PS: Asking help via PM is against the forum rules. Please read the forum rules.

Hello,

Thanks for your reply,

Set DropDownList.AutoPostBack=True and handle the SelectedIndexChanged event.

do i set the auto postback in the properties panel of the dropdown list?

I'll give it a try and see how i get on!!

Ha you're a legend!!!
That's got it working!!
I have been sitting here for hours trying to work that one out, wasn't even close!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.