Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a please select option to top of dropdownlist

Status
Not open for further replies.

raphael232

Programmer
Joined
Jun 9, 2006
Messages
51
Location
GB
Hi, i used the objectdatasource to the bind the dropdownlist control to my table adapter successfull, for example:

Code:
<asp:DropDownList ID="lstCategoryID" runat="server" DataSourceID="ObjectDataSource1" DataTextField="ID" DataValueField="fldCategory">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories" TypeName="KITTableAdapters.tbdCategoriesTableAdapter">
</asp:ObjectDataSource>

but i need a please select option at the top. I'm using visual studio 2005 and i selected edit items for the control and tried adding one but this did not work. My only other way was to put some code in the page load event handler:

Code:
Dim CategoriesAdapter As New KITTableAdapters.tbdCategoriesTableAdapter
Dim Categories As KIT.tbdCategoriesDataTable
Dim Items As New ListItemCollection
Dim Row As KIT.tbdCategoriesRow

Dim TopItem As New ListItem

TopItem.Text = "-- Please Select --"
TopItem.Value = ""
Items.Add(TopItem)

Categories = CategoriesAdapter.GetCategories()

For Each Row In Categories
    Dim Item As New ListItem

    Item.Text = Row.fldCategory
    Item.Value = Row.ID

    Items.Add(Item)
Next

lstCategoryID.DataSource = Items
lstCategoryID.DataTextField = "Text"
lstCategoryID.DataValueField = "Value"
lstCategoryID.DataBind()

but this seems like alot of code to do a simple job. Appreciate your feedback. Thanks
 
There are a few ways. You can add the item in your original SQL Statement, add the item to your datatable, or add the item directly to the DropDownList using the InsertAt method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi cheers for replying so soon (i also tried them guys at asp.net but they're useless). Method 3 sounds good, how do i go about doing that? Also how could i select one of the options on an edit page? I tried the following:

For Each Row In Categories
Dim Item As New ListItem

Item.Text = Row.fldCategory
Item.Value = Row.ID

If Row.ID = News(0).fldCategoryID Then
Item.Selected = True
End If

Items.Add(Item)
Next

but it did nothing. Appreciate your help once more. Thanks
 
after you bind your ddl, simply do this:
[cdode]
lstCategoryID.Items.Insert(0, "-- Please Select --")
[/code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top