473,387 Members | 1,407 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,387 software developers and data experts.

Cant get DropDownList1.SelectedIndex = -1 to work!

Hi,

I've just add a couple of dropdowns to my ASP form, set up a
sqldatasource and bound to the dropdown and it all works great.

I want the first item of the dropdown to be blank soa fter reading a few
articles the DropDownList1.SelectedIndex = -1 seems the way to go.

I've tried setting this oin the asp page and on a page load event in the
vb behind but I still cant get a blank dropdown

Any pointers gratefully received I'm sure I'm missing something simple

Regards

Jim
Jan 12 '07 #1
8 4539
No, you actually need to add an empty item.

You can do it in the database, in the datasource after filling it up from
the database or in the control after databinding it to the datasource.

If you databind to a SqlDataSource, it is easier to add an item to the
database. Otherwise you will need to understand the stages and aspects of
databinding, which is what the SqlDataSource is intended to save you from.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Jim Florence" <fl************@hotmail.comwrote in message
news:45**********************@ptn-nntp-reader02.plus.net...
Hi,

I've just add a couple of dropdowns to my ASP form, set up a
sqldatasource and bound to the dropdown and it all works great.

I want the first item of the dropdown to be blank soa fter reading a few
articles the DropDownList1.SelectedIndex = -1 seems the way to go.

I've tried setting this oin the asp page and on a page load event in the
vb behind but I still cant get a blank dropdown

Any pointers gratefully received I'm sure I'm missing something simple

Regards

Jim

Jan 12 '07 #2
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:O3**************@TK2MSFTNGP03.phx.gbl...
No, you actually need to add an empty item.

You can do it in the database, in the datasource after filling it up from
the database or in the control after databinding it to the datasource.

If you databind to a SqlDataSource, it is easier to add an item to the
database. Otherwise you will need to understand the stages and aspects of
databinding, which is what the SqlDataSource is intended to save you from.
That is precisely why I hate, loathe and abominate all these DataSource
controls in v2 - we're half-way back to FrontPage here...!
Jan 12 '07 #3
"Jim Florence" <fl************@hotmail.comwrote in message
news:45**********************@ptn-nntp-reader02.plus.net...

Jim,
I've just add a couple of dropdowns to my ASP form, set up a sqldatasource
and bound to the dropdown and it all works great.

I want the first item of the dropdown to be blank soa fter reading a few
articles the DropDownList1.SelectedIndex = -1 seems the way to go.

I've tried setting this oin the asp page and on a page load event in the
vb behind but I still cant get a blank dropdown

Any pointers gratefully received I'm sure I'm missing something simple
It's not so much that you're missing something simple, it's more the fact
that you're using the SqlDataSource control... This is supposed to make
database integration "simpler", which it does, but in doing so loses a whole
set of functionality which is possible if you don't use it...

If you were to bind your DropDownList programatically rather than
declaratively, you could accomplish what you require with just one extra
line of code e,g,

MyDropDownList.DataSource = <fetch dataset from database>;
MyDropDownList.DataValueField = <field from dataset>;
MyDropDownList.DataTextField = <field from dataset>;
MyDropDownList.DataBind();

That takes care of the databinding.

Now, to add a blank option at the top of the list, all you need is:

MyDropDownList.Items.Insert(0, new ListItem("", ""));
Jan 12 '07 #4
You don't have to hate them. You can quietly go back to the old good
DataSource databinding and leave xxxDataSource controls for the others who
are (or think they are) happy with them :)

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:OC****************@TK2MSFTNGP03.phx.gbl...
"Eliyahu Goldin" <RE**************************@mMvVpPsS.orgwrote in
message news:O3**************@TK2MSFTNGP03.phx.gbl...
No, you actually need to add an empty item.

You can do it in the database, in the datasource after filling it up
from
the database or in the control after databinding it to the datasource.

If you databind to a SqlDataSource, it is easier to add an item to the
database. Otherwise you will need to understand the stages and aspects
of
databinding, which is what the SqlDataSource is intended to save you
from.
>
That is precisely why I hate, loathe and abominate all these DataSource
controls in v2 - we're half-way back to FrontPage here...!


Jan 12 '07 #5
"Milosz Skalecki [MCAD]" <mi*****@REMOVEITwp.plwrote in message
news:00**********************************@microsof t.com...
You don't need to code it yourself, AppendDataBoundItems is invented
exactly
for this scenario:

<asp:DropDownList runat="server" AppendDataBoundItems="true" ID="ddl"
DataSourceID="ds">
<asp:ListItem Text="Please select..." Value=""/>
</asp:DropDowList>
Thanks for that - I didn't know that was possible...
Jan 12 '07 #6
I managed to fix it by scrapping my initial idea and adding a line in
the code behind to add a blank and set the appenddatabounditems to true
on the dropdown list.

The problem I'm having now is that I can't get the SelectedIndexChanged
procedure to see the DropDownList1.SelectedItem.Text, it returns a blank
and it's definitely being selected.

The plot thickens

Jim Florence

Jim Florence wrote:
Hi,

I've just add a couple of dropdowns to my ASP form, set up a
sqldatasource and bound to the dropdown and it all works great.

I want the first item of the dropdown to be blank soa fter reading a few
articles the DropDownList1.SelectedIndex = -1 seems the way to go.

I've tried setting this oin the asp page and on a page load event in the
vb behind but I still cant get a blank dropdown

Any pointers gratefully received I'm sure I'm missing something simple

Regards

Jim
Jan 12 '07 #7
"Jim Florence" <fl************@hotmail.comwrote in message
news:45**************@hotmail.com...
>I managed to fix it by scrapping my initial idea and adding a line in the
code behind to add a blank and set the appenddatabounditems to true on the
dropdown list.
Sounds like you're mixing the declarative method and programmatical method
of binding your data...
The problem I'm having now is that I can't get the SelectedIndexChanged
procedure to see the DropDownList1.SelectedItem.Text, it returns a blank
and it's definitely being selected.
Have you tried following Milosz' advice and adding the "default" blank
option inside the <asp:DropDownListtag...?
Jan 12 '07 #8
I managed to fix it by scrapping my initial idea and adding a line in
the code behind to add a blank and set the appenddatabounditems to true
on the dropdown list.

The problem I'm having now is that I can't get the SelectedIndexChanged
procedure to see the DropDownList1.SelectedItem.Text, it returns a blank
and it's definitely being selected.

The plot thickens

Jim Florence

Jim Florence wrote:
Hi,

I've just add a couple of dropdowns to my ASP form, set up a
sqldatasource and bound to the dropdown and it all works great.

I want the first item of the dropdown to be blank soa fter reading a few
articles the DropDownList1.SelectedIndex = -1 seems the way to go.

I've tried setting this oin the asp page and on a page load event in the
vb behind but I still cant get a blank dropdown

Any pointers gratefully received I'm sure I'm missing something simple

Regards

Jim
Jan 12 '07 #9

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

Similar topics

3
by: % =joe % | last post by:
I cannot get this code to work. Very simple...I have three list menus. I want to do a check before the form submits to make sure that the value of the 3 fields is equal to 12. Here's my...
4
by: Emil | last post by:
Can somebody tell me what would be the syntax for having an if statement and setting the selected index of a radiobuttonlist? This is my first project using ASP.net and I use C#. I have a repeater...
2
by: MrSmoofy | last post by:
Ok I'll start out with I found: http://support.microsoft.com/kb/327244/ BUG: ComboBox Does Not Clear When You Set SelectedIndex to -1 Ok so in HTML you can have: <select id="MyList">...
5
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the...
4
by: Badass Scotsman | last post by:
Hello, I have some "runat server" form drop downs on the page, for example: <asp:DropDownList ID="TypeOfParrot" Runat="server" name="TypeOfParrot" class="textbox"> <asp:ListItem...
3
by: Alec MacLean | last post by:
Hi, I have a couple of win forms where I am editing values that are stored in a SQL database. I'm using the listbox control to hold the data object each form interacts with. Each object is...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
18
by: Ben | last post by:
Hi, i dynamically feed a dropdownlist which value from 1 to 20. That dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'. There are 4 records and the values of field 'wa' are: 2...
1
by: xtremebass | last post by:
Hello Bytes, i have a calender program which is created by using Javascript. when i execute that program using Internet Explorer,it works properly but when i tried in Mozilla firefox it didnt...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.