databound drop-down-lists: extra items?

L

Lauchlan M

Hi

I have a drop down list, populated from a dataset from a table in a
database.

Say the table was called Locations with fields LocationName and LocationID,
and I was using LocationName for the drop down text and LocationID for the
drop down value.

Now I could select Locations with my drop down list. But suppose I wanted
additional items in the drop down list, such as 'All locations' or 'None',
but I didn't want to store these in my database table (these are not real
locations after all!). I still want my list to be databound but want to be
able to add an item before the databinding and an item after the
databinding.

How do I do that?

Thanks!

Lauchlan M
 
T

Teemu Keiski

After databinding insert the item to the DDL in code.

....
ddl.DataBind()
ddl.Items.Insert(0,New ListItem("None","0"))
....

Inserting item before databinding won't work because databinding first
clears all items and then fetches the data from data source. Using Insert
method shown above you can insert extra items.
 
G

Guest

Hi
I have the same issue as Lauchlan, and I did accordign to Your advice, but after opening the page the drop down list seems to have only the value I added AFTER databinding
I did as You wrote: I bound my data to the drop down list, and after callin
ddl.DataBind(
I tried to insert values that were not from the database (in this case, "all" except "none" as in Your example
But it did not work. All I got is the ddl showing the latest inserted value, not the values from the database at all

What did I do wrong

Kaspia

----- Teemu Keiski wrote: ----

After databinding insert the item to the DDL in code

...
ddl.DataBind(
ddl.Items.Insert(0,New ListItem("None","0")
...

Inserting item before databinding won't work because databinding firs
clears all items and then fetches the data from data source. Using Inser
method shown above you can insert extra items
 
L

Lauchlan M

Hi,
I have the same issue as Lauchlan, and I did accordign to Your advice, but
after opening the page the drop down list seems to have only the value I
added AFTER databinding.
I did as You wrote: I bound my data to the drop down list, and after calling
ddl.DataBind()
I tried to insert values that were not from the database (in this case,
"all" except "none" as in Your example)
But it did not work. All I got is the ddl showing the latest inserted
value, not the values from the database at all.
What did I do wrong?

It worked for me. If it helps, here is my full code for loading the drop
down list:

<<
nxConnection.Open();
nxDataAdapter_HospitalList.Fill(nxDataAdapter_HospitalList_DataSet,
"hospitals");
lstHospital.DataSource = nxDataAdapter_HospitalList_DataSet;
lstHospital.DataMember = "hospitals";
lstHospital.DataTextField = "HospitalName";
lstHospital.DataValueField = "HospitalCode";
lstHospital.DataBind();
nxConnection.Close();
ListItem FirstItem = new ListItem("All hospitals","*");
lstHospital.Items.Insert(0,FirstItem);
I was using the NexusDB Database (www.nexusdb.com) with their ADO.net
provider (http://www.nexusdb.com/index.asp?id=78) for the data access, but
otherwise everything is standard. I just seperated the list item out for
readability and to keep clear where I was setting the index and where I was
setting the value, but I assume it would work if one put them back together
as in the previous example in the thread. The "0" as the first parameter of
the items.insert is specifying the position where the item should be
inserted, so perhaps this is one you need to check?

HTH,

Lauchlan M
 
G

Guest

Thanks

Your code helped. I made a mistake in my code - I found it when I compared it with Yours

Thank You

Kaspian
 

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

Top