473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Inserting a default ListItem in DropDownList Control

I want to insert a default null ListItem with the text "choose an item ..."
in my DropDownList control. I have seen similar postings here regarding this
issue but all of them seem to focus around having to populate datasources,
datatables etc. and then bind the DropDownList in the codebehind of the .aspx
page. I am using Visual Studio 2005 and the .NET Framework 2.0. I thought
that with all these new improvements we could avoid all of the irritations of
..NET Framework 1.x and the weak controls that came with the IDE by being able
to automate all of this databinding stuff. It seems like the only way to
insert individual ListItems is if you go back to doing everything in code.
What gives?

Thanks in advance.
Jan 13 '06 #1
1 1466
[...databinding code...]

ListItem Choose = new ListItem("Choose..."," ");
this.ddlMyDropDownList.Items.Insert(0,Choose);

The 0 in Items.Insert is the item index in the dropdown list. Also, you
must use a space within the two quotes for it to work properly.

The above will create a "Choose..." at the top of your databound
drop-down list (which is great as long as you databound only for
purposes of populating the list).

I prefer to create a blank one instead...new ListItem(" "," ")...and
then use client-side
javascript...document.all['ddlMyDropDownList'].selectedIndex != 0...to
validate that something has been chosen.

Jan 14 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chumley the Walrus | last post by:
I'm trying to add a drop down list to my data insertion script, using the sqlparamter object . The form coding on the web page: <asp:dropdownlist id="frmstate" runat="server"> <asp:ListItem...
2
by: Aymer | last post by:
Scenario: i have a drop down list control in my form. i am binding it with a dataset. that dataset contains the value 1, 2, 3, ..., 10. so when the dropdownlist control is loaded, it contain...
1
by: Moe Sizlak | last post by:
Hi There, I want to bind the value of a field from a database to make a dropdownlist default to that value, I can't seem to get to work correctly. It just doesn't seem work no matter what I do,...
2
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the...
8
by: barry | last post by:
Have tried a number of things including space(n) in front of the string but when the DropDownList is shown the spaces have apparently been trimmed. Is this something that would have to be done in a...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
1
by: IMB | last post by:
I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding in VB.NET. I understand that I can add an item to a databound dropdownlist and make it the default selection using the...
3
by: dizzy77 | last post by:
I have a dropdownlist databound to table column with string values "n" i "y". <asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'> ...
4
by: =?Utf-8?B?SmFwZQ==?= | last post by:
Can I refer to the controls on default.aspx from the masterpage? I have a form on the masterpage which sends information that is in a gridview in the default.aspx page.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.