Cant get DropDownList1.SelectedIndex = -1 to work!

  • Thread starter Thread starter Jim Florence
  • Start date Start date
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
 
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.
 
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...!
 
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("", ""));
 
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 :)
 
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...
 
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
 
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...?
 
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
 
Back
Top