Hi Steve,
After you fill the DDL from the database, user the Items.Insert method to
put in a new instruction item. The Insert method takes the Index value which
is the position in the list where the new item should go.
Some sample code below. Let us know if this helps?
Ken
Microsoft MVP [ASP.NET]
<asp:dropdownlist id="fm_Category" runat="server" >
</asp:dropdownlist>
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
fm_Category.DataSource = CreateDataSource()
fm_Category.DataTextField = "CatName"
fm_Category.DataValueField = "CatID"
fm_Category.DataBind()
Dim lsItem As New ListItem
lsItem.Text = "Please pick an option"
lsItem.Value = "0"
fm_Category.Items.Insert(0, lsItem)
End If
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("CatID", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("CatName", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
"Lastie" <La****@discussions.microsoft.com> wrote in message
news:F3**********************************@microsof t.com...
Hi all,
I’ve got a ‘dropdownlist’ web control and I can add ‘listitem’ no problem.
I
can also bind data from an SQL database fine. My problem is that I want
to
do both at the same time to allow me to have the first option in the list
a
‘listitem’ saying something like ‘please pick an option’, and then the
rest
of options coming from the database.
<asp:dropdownlist id="fm_Category" runat="server" DataTextField="CatName"
DataValueField="CatID">
<asp:ListItem Selected="true">please pick an option</asp:ListItem>
</asp:dropdownlist>
The options from the database seem to take priority and the ‘listitem’ is
not shown at all. Doing this in classic asp was easy as you could
obviously
just write two ‘options’ and then loop the second one with data from the
database.
This is what I’m looking for:
<select name="fm_Category" id="fm_Category">
<option value="">please pick an option</option>
<option value="1">Data from db 1</option>
<option value="2">Data from db 2</option>
<option value="3">Data from db 3</option>
</select>
I’ve only just started to learn asp.NET having come from classic asp so I
hope I’m not just being stupid with this. Any help would be great?
Thanks
Steve