How do i: Databinding 2 database columns to a single dropdown control

J

John Blair

Hi,

I want to select an id column and a name column from the database
and have the results databound to a SINGLE dropdown control with the value
property matched to the database id column and the name values on
display to the user. Any idea how i can do this?

Sample HTML showing desired result: Thanks for any help!

<select name="DropDown1" id="DropDown1">
<option value="1">IN</option>
<option value="2">KS</option>
<option value="3">MD</option>
<option value="4">MI</option>
<option value="5">OR</option>
<option value="6">TN</option>

</select>
 
E

Eliyahu Goldin

If you get the data into a dataset DSStates with a table tblStates, then
the dropdownlist will look like the following:

<asp:dropdownlist id=ddlLocation Runat="server" DataTextField="Name" DataValueField="Id"
DataMember="tblStates" DataSource="<%# DSStates %>"></asp:dropdownlist>

Eliyahu
 
J

John Blair

Thanks alot....right again!!!!!!
I wanted the intial selection to be nothing .listindex=-1 in vb terms but it
always seems to select first item even if i reset listindex to -1 after
databinding.
HAve i got to load a blank entry as the first item in the dropdownlist?

Thanks a lot!
 
J

John Blair

Hi,

I found this code snipet to do the trick after data binding!

DropDownList1.Items.Insert(0, New ListItem("", "-1"))



Thanks!
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top