• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

putting 'Select Item' in a dropdownlist in vb.net 2003

vb.net 2003 ,  I am populating a dropdown list from my sql server.  When I run my web page, it will list the first item, I prefer to have some text in there like 'Select Item',  how would I put some generic text in the dropdownlist while pulling the items from sql server?    
1 Solution
After you have bound the dropdown list to the data, try this to insert at top.........
ddl.Items.Insert(0, New ListItem("Select.....", ""))
When you have bound the results from the query to the dropdown.

MyDropDownList.Items.Insert(0,New ListItem("Select Item",-1))

To add an item at the top of the list. If you prefer a different value to "-1" then change it accordingly.
When you postback, you'll have to now validate the value of the drop down list, confirming that it isn't blank.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

bergertimeAuthor Commented:
Works great, but.......say the user has selected value 4, now in the ddl I have  "Item"  as my top item, can I prevent the user from selecting it?  Would I put an if statement in the value change event to prevent it?  I understand most of my users will see "Item" and want to click on it instead of all the listed items.  Thanks
first of all I am not sure you understand that the items.insert statement has to be within a check for postback
if not ispostback() then
  ddl.Items.Insert(0, "Select")
end if
If you knew that, please ignore my comment ..

>>Would I put an if statement in the value change event to prevent it?
is the dropdown a required field .. if yes add a required field validator with "initialvalue" property of the validator set as "Select"
else if this is not a required field validator, you will have to use a custom validator which will check if the selected value = "select"

there are other fancy ways of making the user not able to able to choose "select" using javascript onblur client side event and checking if vlaue = "select", then change it back to some other value ..

You can view in this way to bind you dropdownlist.So that your selected value will be "Select" at first.
ds is the DataSet from database.                  
DataView dvList;
DataRowView drv;
dvList = new DataView(ds.Tables[0]);
dd.DataSource = dvList;
drv = dvListDist.AddNew();
drv["one"] = "Select";
drv["two"] = "0";                        
dd.DataTextField = "one";
dd.DataValueField = "two";
dd.Selectedvalue = ds.Tables[0].Rows.Count-1;
Trying to see the difference between the accepted answer and mine..................... Nope think they're identical!! :-S
bergertimeAuthor Commented:
You are correct, I was in a hurrry to clean up any open questions I had, is there any way I can make it up to you?
Nah, don't worry. Was just curious why :-)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now