By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,940 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,940 IT Pros & Developers. It's quick & easy.

How do I bind to a dropdownlist as well as put my own line in?

P: n/a
I'm leafing through a big book on, and I don't see any way to
the following:
1. bind values and text to a dropdown
2. Also make the first line of the dropdown say something different.
For instance, if I'm working with a table of hotels, I might want a
'select' control as follows
<select id="mylistbox" runat="server">
<option value="*">Add New Hotel</option>
<option value="1">The Hilton</option>
<option value="2">The Waldorf</option>
The first choice in this listbox is not in a database, its my own text.
The second and third (and so on) choices come from a database. Can
anyone give me a code snippet that can produce the above?
Thanks in advance,

Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a

Please try this:

private void BindDD()
ddlHotels.DataSource = dsMyDataset;
ddlHotels.DataMember = "TableNameInDataset";
ddlHotels.DataTextField = "TheFieldInTheTableToAppearInDD";
ddlHotels.DataValueField = "TheFieldInTheTableToBecomeValueInDD";
ddlHotels.Items.Insert(0, "MyCustomText");


Nov 19 '05 #2

P: n/a
&lt;asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="DataSource" DataTextField="HotelName" DataValueField="HotelId" SelectedValue='&lt;%# Bind ("HotelId") % &gt;'&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:ListItem Value="0"&gt;Add Hotel&lt;&frasl;asp :ListItem&gt;

Make sure you primary Keys&frasl;Ids for each table start at Identity 1 and not 0, since this would cause problems with AddHotel being value of 0, and the only reason you want all tables to start at 1, is to keep it all the same since you don't want some tables to start at 0, and others at 1, since that would be confusing.

So this will generate something like this as rough sample

&lt;asp:DropDownList ID="DropDownList1" runat="server"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:ListItem Value="0"&gt;Add Hotel&lt;&frasl;asp:ListItem&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:ListItem Value="1"&gt;Anothe rHotel&lt;&frasl;asp:ListItem&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:ListItem Value="2"&gt;And Another Hotel&lt;&frasl;asp:ListItem&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:ListItem Value="n"&gt;N etc...&lt;&frasl;asp:ListItem&gt;

Since we put
This is what people would see in the DropDownList

Since we put
This is what is the Value="" would be

Since we put
SelectedValue='&lt;%# Bind("HotelId") %&gt;'
This is what will be stored, not the hotelName, but the HotelId for it that table

I'm guessing you have like a table with like maybe


And then another table something like I don't know Hmm??

&nbsp;&nbsp;&nbsp;&nbsp; --EmployeeId
&nbsp;&nbsp;&nbsp;&nbsp; --Name
&nbsp;&nbsp;&nbsp;&nbsp; --HotelId

So you would load info in a table about employees, but it's not usefull
for people to see the Foreing Key of the Employees HotelId, since you could have like
thousands of hotels, and that would be hard for people to rember all that suff, and
they would always have to have Hotels table as reference, this is why you made the DropDownlist ListItems Value=Ids

Since you have two tables Hotels, and Employees

You could make a select like this so instead of getting the Foreing key you actualy get the data the hotelName.

SELECT Employees.EmployeeId, Employees.Name, Employees.HotelId, Hotels.HotelName
Hotels ON Employees.EmployeeId = Hotels.HotelId
ODER BY Employees.Name ASC

This way you will not get something like this which users would not like
&nbsp;&nbsp;&nbsp;&nbsp;EmployeeId | Name | HotelId
&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbs p; | Foo Bar | 20
&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbs p; | Boo Bar | 34

You should get something like this
&nbsp;&nbsp;&nbsp;&nbsp;EmployeeId | Name | HotelId | HotelName
&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbs p; | Foo Bar | 20 | The dock
&nbsp;&nbsp;&nbsp;&nbsp;2 &nbsp;&nbsp;&nbsp;&nbsp; Boo Bar | 34 | The Inn

This way you can make a datasource, bind it to a gridview,
and edit the gridview to remove columns like EmployeeId, and HotelId.
So this way you will only see Name, and HotelName on the gridview, but you still have the other infos like EmployeeId, and HotelId to play around with for commands like delete and edit&frasl;update.

Want to see what I mean, just ask. I'll show you some of my source code
where I load info in a gridview, and use a formview to add&frasl;edit&frasl;update&frasl;delete records.


Posted via Groups
Sep 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.