Saturday, March 24, 2012

populate dropdownlist

I have a list which I am trying to populate from my db, I can my items but not my unique id, my code is below

_cmd.CommandText = "SELECT workgroupID, workGroupname FROM dbo.workgroup"
_dr = _cmd.ExecuteReader



While _dr.Read
workgroupSelect.Items.Add(_dr("workgroupname"))
workgroupSelect.DataValueField = _dr("workgroupid")
End While


How do I get my id field into the list so that I know which one was selected??

How do I know which one was selected?

Grateful for any help.

Cheersok, I now have
With workgroupSelect
.DataSource = _dr
.DataTextField = "workgroupname"
.DataValueField = "workgroupid"
.DataBind()
.Items.Insert(0, "<- Select a Work Group ->")
End With

so all I need now is to know is the workgroupId of the one selected??

Cheers
try :

workgroupSelect.SelectedItem.Value
I have tried that,

I have ..

Protected Sub workgroupSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles workgroupSelect.SelectedIndexChanged
Dim strselected As String
strselected = workgroupSelect.SelectedValue.ToString
Label1.Text = strselected
End Sub

but the only I can get the label text to show the value is to set the dropdowncontrol to postback = true
is there a way around this??
is the value actually changing even if I cant see it in the label??

Thanks
The value would change but there is no way to see it refreshed unless the postback is activated. Such is web development :sick:

0 comments:

Post a Comment