selected item and dropdown list/listbox

R

rohith

Hope someone can help with this.

I am trying to add items to a drop down list from a
database. I can do this fine by using the bind method.
What I want to do also is to set the selected item to one
of those values. Using Asp 3.0 I could use the While
Wend loop to see if the value from the database equals
the variable and if so make this the selected item

While
If rs("x") = x Then
<option selected>rs("x")</option>
End If
Wend

Is there similar code in Asp.net

Thank you in advance.
 
T

Tapasvi Mehta

Rohith,

This is what i normally do, and it works fine for me.
Comparing the value to the value in dropdownlist box, and
setting the SelectedIndex property to the current counter.

Here ProjectItemType is the dropdownlist box.

----------------------------------------------------------
For ItemTypeCounter = 0 To ProjectItemType.Items.Count - 1
If ProjectItemType.Items(ItemTypeCounter).Value
= "Value" Then

ProjectItemType.SelectedIndex = ItemTypeCounter
Exit For
End If
Next ItemTypeCounter
 
D

Deasun

Good evening,

after the bind do the following;
..ClearSelection()
..SelectedValue = ({Your value to find as string})
 
R

rohith

Thank you very much, this works.

-----Original Message-----
Rohith,

This is what i normally do, and it works fine for me.
Comparing the value to the value in dropdownlist box, and
setting the SelectedIndex property to the current counter.

Here ProjectItemType is the dropdownlist box.

--------------------------------------------------------- -
For ItemTypeCounter = 0 To ProjectItemType.Items.Count - 1
If ProjectItemType.Items(ItemTypeCounter).Value
= "Value" Then

ProjectItemType.SelectedIndex = ItemTypeCounter
Exit For
End If
Next ItemTypeCounter
---------------------------------------------------------

Thanks,

Tapasvi

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top