drop down list

J

JP.Gantlin

I have a drop down list that pulls the names of States from a database.

To this list I want to add "All", so the page will search all states.

how do I combine the manually-added "All" with the list of states from the
database?


Also how would i parse that so the sql string uses * or something

thanks
 
M

Mark Rae

I have a drop down list that pulls the names of States from a database.

To this list I want to add "All", so the page will search all states.

how do I combine the manually-added "All" with the list of states from the
database?

You need the Items.Insert method of the DropDownList to add a new ListItem
at the index you specify...
 
J

JP.Gantlin

at what point? Page load?

if i do that it loads "all" over and over with each page load
if i use this , it never laods it:



If Not IsPostBack Then

DropDownList2.Items.Insert(1, "All")

End If
 
M

Mark Rae

at what point? Page load?

No - you should populate DropDownLists in Page_Init

DropDownList2.Items.Clear
DropDownList2.DataSource = <dataource>
DropDownList2.Items.Insert(1, "All")
 
J

JP.Gantlin

Well that works on DropDownList1 (countries) *but* DropDownList2 (states)
no longer works

DropDownList2 pulls its data based on what is selected in DropDownList1 .


What do you think?



DropDownList1.Items.Clear()

DropDownList1.DataSource = SqlDataSource1

DropDownList1.DataBind()

DropDownList1.Items.Insert(0, "All")

DropDownList2.Items.Clear()

DropDownList2.DataSource = SqlDataSource2

DropDownList2.DataBind()

DropDownList2.Items.Insert(0, "All")
 
M

Mark Rae

Well that works on DropDownList1 (countries) *but* DropDownList2 (states)
no longer works

DropDownList2 pulls its data based on what is selected in DropDownList1 .
What do you think?

DropDownList1.Items.Clear()
DropDownList1.DataSource = SqlDataSource1
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "All")
DropDownList2.Items.Clear()
DropDownList2.DataSource = SqlDataSource2
DropDownList2.DataBind()
DropDownList2.Items.Insert(0, "All")

Well obviously not!

If the Items collection of the second DropDownList is dependent on the
selected item of the first, then you have three choices:

1) In Page_Init, query all the possible options for DropDownList2 and pump
them into a client-side JavaScript array. Then, when the selectedIndex of
DropDownList1 changes, repopulate DropDownList2 appropriately through
client-side JavaScript. Bigger hit when the page is loaded initially, but
avoids subsequent postbacks...

2) Wire up an OnSelectionChanged server-side event for DropDownList1, set
its AutoPostBack property to true, and requery the contents of DropDownList2
server-side. Much smaller initial hit, but will need a postback every time
the selectedIndex of DropDownList changes.

3) Use Ajax. Sort of a combination of the first two options...
 

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

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top