0

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

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by Goomba79
0

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.

Edited by __avd

0

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!!

0

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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.