How to make first combo box entry blank?

R

Randall Parker

Using DropDownList to create select/option tags in HTML how to make the first option
be a blank string?

I have fields with optional values. Currently I'm populating the DropDownList by
querying out a list of choices from a table. But the table shouldn't have a blank row.

A couple of possibilities:

1) Use a UNION query. I've done this in MS Access with combo boxes and the first
member of the union returns a single row. I can't remember the SQL syntax for
returning a single row that is not from a table though. Anyone know how?

2) Populate the DropDownList in CodeBehind without a bind of a result set. Just add a
blank row and then do a foreach on the dataset that has the real rows and add all them.

3) Some other approach?
 
J

Jason Kester

#1 requires your datalayer to know about presentation, so that's out.

#2 works fine, and will score you points with oldskool purists and php
kids, but it's extra work compared to...

#3 get your dataset from the database and cram an extra row into it
before binding:


private void PopulateFamilyList()
{
DataSet ds = Family.List();
ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = Family.Columns.FamilyName;

selFamilyID.DataSource = dv;
selFamilyID.DataValueField = Family.Columns.FamilyID;
selFamilyID.DataTextField = Family.Columns.FamilyName;
selFamilyID.DataBind();
}


Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
S

Siva M

After data binding, DropDownList.Items.Inser (0, "")

"Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit.com>
wrote in message Using DropDownList to create select/option tags in HTML how to make the
first option
be a blank string?

I have fields with optional values. Currently I'm populating the
DropDownList by
querying out a list of choices from a table. But the table shouldn't have a
blank row.

A couple of possibilities:

1) Use a UNION query. I've done this in MS Access with combo boxes and the
first
member of the union returns a single row. I can't remember the SQL syntax
for
returning a single row that is not from a table though. Anyone know how?

2) Populate the DropDownList in CodeBehind without a bind of a result set.
Just add a
blank row and then do a foreach on the dataset that has the real rows and
add all them.

3) Some other approach?
 
R

Randall Parker

Jason,

I like the idea of inserting a row in the dataset. But why are you doing a Sort? If
I've already queried out by dataset in my preferred sort order is it unnecessary and
perhaps even unwise to do the sort?

Also, when you do this:

ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);

I do not see how the column in the new row ever gets set to an empty string. Is that
just by default?

Mind you, I'm an ADO.Net novice along with being an ASP.Net novice (I'm coming from
C++ and Java and know ODBC, JDBC, DAO, ADO and a few other database api sets fwiw).
So maybe I'm missing something obvious.
 
G

Guest

I'd recommend against this. DataBound controls flip out sometimes when
you modify them after binding.

Jason , just curious on this point .. I got a page with 17 dropdown lists
(users choice!) databound and then a "Please select..." option added with a
code similar to below.. did not face any problem so far.. have you come
across any specific issues?
 
R

Randall Parker

Jason,

Here's what seems to work for me. Does this seem right to you?

dbCmd.CommandText = "SELECT brand_name FROM mydb.BrandName ORDER BY
brand_name";
DataSet dsBrandNameDataSet = new DataSet();
dbAdapter.SelectCommand = dbCmd;
dbAdapter.Fill(dsBrandNameDataSet);

// Insert a blank row here.
DataRow TopRow = dsBrandNameDataSet.Tables[0].NewRow();
TopRow["brand_name"] = "";
dsBrandNameDataSet.Tables[0].Rows.InsertAt(TopRow,0);

BrandNameList.DataSource = dsBrandNameDataSet;
BrandNameList.DataTextField = "brand_name";
BrandNameList.DataBind();
 
J

Jason Kester

"Sometimes" is the operative word here. "Occasionally" might even have
been a better choice.

I ran into this a couple years back, and switched to the other method
with better success. If it's working for you, then I wouldn't sweat
it. Just something to keep in mind if those boxes start acting up in
the future!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
J

Jason Kester

Sorry if my code was misleading. I just pasted a fragment from one of
my pages. I never use ad-hoc SQL (and would recommend you avoid it
too), so I sometimes find it easier to sort the results of a Stored
Procedure call locally, provided it's only a few records.

Looks like you're headed in the right direction. You're correct in
assuming that .NewRow()s come in blank, so the approach you take would
be the only way to set the text to "-- please select a value --" or
something similar.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
G

Greg Burns

Just wanted to say I always do it after databinding. By using the
DropDownList.Items.Insert(0, "-- please select a value --") trick. Never
had a problem doing it that way. Also, I would think it is kinda necessary
if binding using a datareader...

Greg
 
M

Mr Newbie

There is no reason why there should be a problem 'AFAIK'. Databinding is
only single direction on ASP.NET so I really dont see why this slould be an
issue once the page has been rendered

By contrast, try doing with with a Windows Forms application and things get
a little more tricky !

Regards Mr N . . .
 
J

Jason Kester

You know what, I bet you're right. I probably came across that doing
winform development, and was so traumatized that it just left this Big
Black Mark of Dread in my consciousness. "Here Be Dragaons" and all
that, and it's haunted me all the way to web development!

So yeah, I'll agree that I was probably talking out my arse a few posts
back. Bind away!

Jason
 
G

Guest

I am using Visual Studio 2005 and by using the new datasource wizard I have
been able to cut back on alot of coding (i.e. I don't have to perform any of
the databinding stuff in by codebehind). I am now faced with this issue where
I can't add a balnk row unless I do it programatically. I would think that
with all of the improvements made to the Visual Studio 2005 IDE and to the
..NET Framework 2.0 that Microsoft would not have left out the ability to
accept hard-coded ListItems along with ones that were retrieved from a data
store after calling the databind() method. Anyone know how to do this?
 
Joined
Sep 10, 2006
Messages
1
Reaction score
0
This worked for me to enter a blank first entry followed by the databound data, with no extra code behind

<asp: DropDownList ID="ddlEvents" runat="server" DataSourceID="EventsDS" DataTextField="event_name" DataValueField="event_id" AppendDataBoundItems="True">
<asp:ListItem></asp:ListItem>
</asp: DropDownList>

The empty ListItem ends up being the blank entry at the top of the DropDownList


Greg Burns said:
Just wanted to say I always do it after databinding. By using the
DropDownList.Items.Insert(0, "-- please select a value --") trick. Never
had a problem doing it that way. Also, I would think it is kinda necessary
if binding using a datareader...

Greg
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top