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

  • Thread starter Thread starter John Blair
  • Start date Start date
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>
 
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
 
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!
 
Hi,

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

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



Thanks!
 
Back
Top