Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

determining if a listbox item is picked

Status
Not open for further replies.

zma

Programmer
May 30, 2006
25
US
On page_load I am filling a listbox by looping through a dataset, LISTBOX1.ITEMS.ADD(NEW ...) THe listbox fills up ok.

On a button click I'm looping through it and checking which rows have been highlighted using IF LISTBOX1.ITEMS(I).SELECTED=TRUE
...END IF. It is always evaluating to false no matter if a a row is picked or not.

Why are all rows showing as not picked?





 
Have to ask, are you using the If Not Page.IsPostBack check to be sure you are not repopulating the listbox?


Sub Page_Load()
If Not Page.IsPostBack Then
LISTBOX1.ITEMS.ADD(NEW ...)
LISTBOX1.ITEMS.ADD(NEW ...)
...
End If
End Sub

Sub Button_Click()
IF LISTBOX1.ITEMS(I).SELECTED=TRUE THEN
...
END IF
End Sub
 
I tried

listbox1.datasource=dataset1
listbox1.databind()

but listbox1 appeared with "System.Data.DataRowView"s for each member of the dataset.

 
then there is something else wrong with your code. Post all relevant code
 
You need to set the DataTextField and DataValueField property of the ListBox, you can do this in the codebehind or in the HTML.

listbox1.DataValueField = "ColumnID"
listbox1.DataTextField = "ColumnName"
listbox1.DataSource = dataset1
listbox1.DataBind()
or
<asp:ListBox ID=listbox1 runat=server DataTextField="ColumnName" DataValueField="ColumnID"

You can set just the datavaluefield, as long as they are all unique values and it will display the values as the textfield, however setting both will be your best route.

Your DataTable would be something like

ColumnID ColumnName
-------- -----------
1 John Deere
2 Apple Juice
3 Potatoes
4 Smelling Salts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top