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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

databind dropdown list initial value 2

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I've bound data to dropdownlist that users select and submit button to pass form variable. But I want to create an blank or "---" initial value.

What I'm trying to do is I have several different dropdownlists that users use for searching based on different parameters. If there's a blank or --- value for the field it'll ignore it and won't add it to the sql statement.

Could someone help me out here? Thanx
 
After you databind, you can run the following statement to add a "SELECT ONE" option to the beginning of the list:

drpMyDropDownList.Items.Insert(0, new ListItem("SELECT ONE", "-1));

Where "SELECT ONE" is the text, and "-1" is the value of that option.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Hi there...

I have question about this one . . . :D

I also wanted to have a heading at the top of my drop down list, and i understand that DropDownList1.Items.Insert(0, New ListItem("-- Select User --", "-1"))

will add the value "-- Select User --" and put it at the top of the list, so i thought it would be common sense to add this to the Page_load section, however none the less the value doesnt apear untill a selection is made first time . . . then the ddl is updated with this value? any ideas? what am i doing wrong?

Mark
 
Below: ProductLine is ddl; ProductLine_DataBind() is datasource, end of sub has code for initial value; Databind() goes in Page_load

Sub Page_Load (sender as Object, e as EventArgs)
ProductLine_DataBind()
CustomerList_DataBind()
End Sub

Sub ProductLine_DataBind()
Dim sqldisplay as String
sqldisplay = "SELECT * FROM ProductLine"
Dim conn as OleDbConnection
conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\local\db\Quote.mdb")
conn.Open()
Dim cmd as OleDbCommand
cmd = new OleDbCommand (sqldisplay, conn)
Dim reader as OleDbDataReader
reader = cmd.ExecuteReader()
ProductLine.DataSource = reader
ProductLine.DataTextField = "ProductLine"
ProductLine.DataValueField = "ProductLine"
ProductLine.DataBind()
Dim oitem as ListItem
oitem = new ListItem("Product Line","-1")
ProductLine.Items.Insert(0,oitem)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top