Forum Moderators: open

Message Too Old, No Replies

Static and Dynamic Items in a DropDownList

Is this possible?

         

Argblat

8:27 pm on May 11, 2005 (gmt 0)

10+ Year Member



So I am creating a list of Locations in a drop down which is being pulled from a sql server database:

<asp:DropDownList ID="DDLocations" runat="server" style="width:80%" DataSource="<%# DSLocations.DefaultView %>" DataTextField="LOCATION" DataValueField="LOCATION">
</asp:DropDownList>

So that the final drop down looks something like:

NY
NJ
PA
CT

My question is...is there a way to add something like what you often see in drop down menus such as "Select a Location" as the first option in the drop down, but having that not have to come from the database:

Select A Location:
NY
NJ
PA
CT

I appreciate any help you can offer,
-Mike

TheNige

9:03 pm on May 11, 2005 (gmt 0)

10+ Year Member



If you set up the datasource and databind in the codebehind, you can add items to the dropdown list after the bind happens....so in someplace like your page_load you would do something like this.

DDLocations.datasource = DSLocations.DefaultView
DDLocations.databind
DDLocations.items.insert(0,"Select A Location:")

You may not have to do the datasource and databind in the codebehind....but I'm not sure of the order of events. Doing it all together as I have above guarantees that your inserted item does not get dropped by the databind....experiment.

txbakers

12:34 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From my limited experience, I can concur with The Nige - do the binding in the codeBehind and add the extra line after you bind. That way it happens at the same time.

Then you will always fire the SelectedIndexChanged event in the drop down.