473,396 Members | 2,089 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,396 software developers and data experts.

How to make first combo box entry blank?

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?
Nov 19 '05 #1
12 2495
#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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #2
After data binding, DropDownList.Items.Inser (0, "")

"Randall Parker" <NOtechieSPAMpundit_please@future_avoidjunk_pundit .com>
wrote in message news:%2****************@tk2msftngp13.phx.gbl...
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?
Nov 19 '05 #3
Siva M wrote:
After data binding, DropDownList.Items.Inser (0, "")


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

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

Nov 19 '05 #4
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.

Jason Kester wrote:
#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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #5
> 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?
"Jason Kester" wrote:
Siva M wrote:
After data binding, DropDownList.Items.Inser (0, "")


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

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

Nov 19 '05 #6
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();
Jason Kester wrote:
#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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #7
"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/

Nov 19 '05 #8
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/

---
Get your own Travel Blog, with itinerary maps and photos!
http://www.blogabond.com/

Nov 19 '05 #9
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
"Jason Kester" <ja*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
"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/

Nov 19 '05 #10
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 . . .
"Greg Burns" <bl*******@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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
"Jason Kester" <ja*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
"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/


Nov 19 '05 #11
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

Nov 19 '05 #12
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?

"Randall Parker" wrote:
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?

Jan 13 '06 #13

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

Similar topics

3
by: B | last post by:
I know there are several ways to speed up combo boxes and form loading. Most of the solutions leave rowsource of the combo box blank and set the rowsource to a saved query or an SQL with a where...
6
by: Dos Lil | last post by:
I have a comobox in the main form which has a query attached to it to list the employee numbers.I have written this code to requry the control in the control's after update event Private Sub...
2
by: Danny | last post by:
After I update a combo box, you have to click on the arrow to actually see whats in the list. Is there a way to have the first entry or first row be equal to the first entry in the list? Thanks...
2
by: c.kurutz | last post by:
Hello. I have a data entry form with several combo box fields with dlookup code in the after update procedure for the field. I am trying to add a button to copy the record on this form. I am...
10
by: halfbodyguy | last post by:
I am using a continuous form, with a combo box. When I select an entry, then Click somewhere else on the form, the entry updates and the combo box goes blank. I don't want it to update until i tell...
7
by: aviad | last post by:
I am writing a Form application I need it to fit both resolution of 1600*1200 and 800*600 (and any other resolution that might jump in) the application is meant for regular PCs another question...
3
rhitam30111985
by: rhitam30111985 | last post by:
Hi all.. i am trying to create a combo with the pop down list being modfied in real time as i type each character : import gtk window=gtk.Window(gtk.WINDOW_TOPLEVEL)...
4
klarae99
by: klarae99 | last post by:
Hello, I am working on an Access 2003 Database. The tables that pertain to this issue are tblOrg, tblState, tblCity, and tblZip. I have posted the table structure with only the pertinant fields...
2
kmartinenko
by: kmartinenko | last post by:
Hi everyone, I have several combo boxes on my form, and while I have designated a column head, I cannot figure out how to default to the column head value. What I really want is for all of my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.