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

C

COHENMARVIN

I'm leafing through a big book on asp.net, 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>
</select>
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,
CohenMarvin
 
G

Guest

Please try this:

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

cdmlb

&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;
&lt;&frasl;asp:DropDownList&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;
&lt;&frasl;asp:DropDownList&gt;

Since we put
DataTextField="HotelName"
This is what people would see in the DropDownList

Since we put
DataValueField="HotelId"
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

Hotels-------&frasl;
&nbsp;&nbsp;&nbsp;&nbsp;--HotelID
&nbsp;&nbsp;&nbsp;&nbsp;--HotelName

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

Employees--&frasl;
&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
FROM Employees INNER JOIN
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
Employees--&frasl;
&nbsp;&nbsp;&nbsp;&nbsp;EmployeeId | Name | HotelId
&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp; | Foo Bar | 20
&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp; | Boo Bar | 34

You should get something like this
Employees--&frasl;
&nbsp;&nbsp;&nbsp;&nbsp;EmployeeId | Name | HotelId | HotelName
&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp; | 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

From http://www.developmentnow.com/g/8_2...dropdownlist-as-well-as-put-my-own-line-in.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top