Cant get DropDownList1.SelectedIndex = -1 to work!

J

Jim Florence

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
 
E

Eliyahu Goldin

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.
 
M

Mark Rae

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...!
 
M

Mark Rae

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("", ""));
 
E

Eliyahu Goldin

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 :)
 
M

Mark Rae

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...
 
J

Jim Florence

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
 
M

Mark Rae

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:DropDownList> tag...?
 
J

Jim Florence

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top