all groups > asp.net webcontrols > october 2007 >
You're in the

asp.net webcontrols

group:

use index on dropdownlist


use index on dropdownlist Sam
10/25/2007 10:53:06 AM
asp.net webcontrols:
I got a drop down list, but when i try to selected an index it dose not
work. can someone give me some help?

here the code:

Dim dsCountry As New DataSet

dsCountry = GetCountries()


With dpdnCountry

Dim lstItem As System.Web.UI.WebControls.ListItem
Dim i As Int32

lstItem = New System.Web.UI.WebControls.ListItem
lstItem.Value = ""
lstItem.Text = " -- Select -- "
.Items.Add(lstItem)

For i = 0 To dsCountry.Tables(0).Rows.Count - 1
If (Not dsCountry.Tables(0).Rows(i)("description") Is
System.DBNull.Value) And (Not dsCountry.Tables(0).Rows(i)("description") Is
DBNull.Value) Then
lstItem = New System.Web.UI.WebControls.ListItem
lstItem.Value = dsCountry.Tables(0).Rows(i)("id") '
i.e. 1
lstItem.Text =
dsCountry.Tables(0).Rows(i)("description") ' i.e. USA
.Items.Add(lstItem)
End If
Next

dpdnCountry.SelectedIndex() =
dpdnCountry.Items.IndexOf(dpdnCountry.Items.FindByValue("USA"))

end With
--
Thanks,
Re: use index on dropdownlist Phil H
10/25/2007 1:57:38 PM
Hi Sam

Are you sure you have reproduced this correctly?

[quoted text, click to view]

If so why are you testing the same condition twice?

I'm not sure that the "Is" operator in this context will give the
result you are looking for and is probably the reason it isn't working
(the test fails every time and the list remains empty).

Personally I think it's better to work with DataView and DataRowView
objects for reading and updating tables. I recommend something like
this:

Dim dvCountry as DataView = dsCountry.Tables(0).DefaultView
Dim drvCountry as DataRowView

Then in place of your For-Next loop for building the DropDownList try:

For Each drvCountry in dvCountry

If Not IsDBNull(drvCountry("description")) Then

1stItem = New System.Web.UI.WebControls.ListItem
1stItem.Value = drvCountry("id")
1stItem.Text = drvCountry("description")
.Items.Add(1stItem)

end if

Next

HTH


AddThis Social Bookmark Button