Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 30th, 2004, 08:18 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Drop Down List Control

Does anyone know if you can stop a .NET drop down list from defaulting the selection to the first item in the list ? I have tried both lstGeneric.Items(0).Selected = false and
lstGeneric.SelectedValue = Nothing but with no effect. I want to ensure the user has actively selected something.
 
Old April 30th, 2004, 08:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

This is in the wrong forum. I will move it to the proper one. Please post in the proper forum in the future.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old April 30th, 2004, 08:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Now for the answer- A drop down must have something selected. The normal way to "trick" this is to create a blank entry as the first one in the dropdown- or something that says "Select" as the default. Then check for it and if it's not changed- you know they didn't select something.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old April 30th, 2004, 08:58 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Hal - sorry for posting in wrong forum !

 
Old May 6th, 2004, 08:13 AM
Authorized User
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can I add this "select" item as item(0) in the list after having populated the list from a DB with code as below:

         lstGeneric.DataSource=GetGeneric()
         lstGeneric.DataValueField = "ID"
         lstGeneric.DataTextField = "Generic_Name"
         lstGeneric.DataBind()

*** Now I want to add my "---- Please Select ----" item here - how ?

 
Old May 6th, 2004, 08:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

WHen I do somthing like this I usually have it in the database.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old May 6th, 2004, 08:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rob,

You can use the Insert method of the Items collection of the list control to add an item at a specified location, 0 in this case:
Code:
lstGeneric.Items.Insert(0, new ListItem("Please select an item", ""));
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Something In The Way by Nirvana (Track 12 from the album: Nevermind) What's This?
 
Old May 6th, 2004, 08:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Imar, Won't the databind erase the item you manually add? That's why I always put my "null" in the database.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old May 6th, 2004, 09:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

No, it won't if you add the item *after* you have bound the data to the control.
That is also the reason you need to use the Insert method, not the Add method. The Add method will add the item at the end of the list. This is good if the list is still empty (it will be added at position 0). However, when you bind the control to its DataSource, this single item is cleared.

When you use the Insert method, you can add the item at position 0 (or wherever you want) and still leave the original items from the DataSource intact.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Something In The Way by Nirvana (Track 12 from the album: Nevermind) What's This?





Similar Threads
Thread Thread Starter Forum Replies Last Post
drop down list ashu_gupta75 Excel VBA 1 October 15th, 2007 05:52 PM
Drop down list carumuga Classic ASP Basics 1 February 6th, 2007 02:37 PM
Drop Down List Control Issue PrinceAli0 ASP.NET 2.0 Basics 0 June 9th, 2006 01:46 PM
drop down list values based on another drop down noor ASP.NET 1.0 and 1.1 Basics 3 July 5th, 2005 09:57 AM
Drop Down List Beginner123 VB How-To 2 March 7th, 2005 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.