Forums
This topic is locked
add static item to dynamic drop down (asp.net c#)
28 Jul 2005 18:22:42 Mike M posted:
Hi all, this is my first post on this forum, so here goes nothing.What I need to be able to do is to add a static element to an asp.net drop down list that is being dynamically generated via a database.
For example, let's say that I have a table in my database containing all of the States and their 2 digit state codes (AL, AK, etc etc)
I go into Dreamweaver and create a DataSet that returns just the 2 digit codes...no problem.
Then I create an AspropDownList and assign the ListItems to be from a database, and select the prior mentioned dataset.
My drop-down list will now be created and the first item in the drop down will be the first item coming back from the database.
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>
<% STATE.SelectedIndex = STATE.Items.IndexOf(STATE.Items.FindByValue(""); %>
<asp:dropdownlist id="STATE"
DataSource="<%# DataSet_DistinctState.DefaultView %>"
DataTextField="STATE"
DataValueField="STATE"
runat="server">
</asp:dropdownlist>
<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
What I want to be able to do is insert a ListItem (manually, by hand, and static)...something like "Please select a state..." with a value of blank or null.
How can I, given the code that dreamweaver has created in order to create the dynamic drop down, add the static element that I need to add.
Thank you in advance to anyone who can offer me some help,
Mike
Replies
Replied 28 Jul 2005 23:06:10
28 Jul 2005 23:06:10 Mike M replied:
::: I wonder, if I told everyone that Macromedia support said that this was impossible, if it would scare them all off...or make them want to help me figure it out more :::
Replied 08 Aug 2005 18:36:42
08 Aug 2005 18:36:42 myke black replied:
couple of ways you can do this in asp, not sure how to do it in dreamweaver, but if you do this:
<% STATE.SelectedIndex = STATE.Items.IndexOf(STATE.Items.FindByValue(""); %>
<asp:dropdownlist id="STATE"
DataSource="<%# DataSet_DistinctState.DefaultView %>"
DataTextField="STATE"
DataValueField="STATE"
runat="server">
<asp:ListItem Value="">Please select an option</asp:ListItem>
</asp:dropdownlist>
it might work.
Alternatively, if that doesnt work, then add this to the code behind portion of your page:
STATE.Items.Add("Please Select"
or you could try making a post page loading javascript to add an option at the top of the select list,
or if all else fails, change the code from an asp:droplist to a asp:repeater and format the output into option tags, and add the static one at the top.
<% STATE.SelectedIndex = STATE.Items.IndexOf(STATE.Items.FindByValue(""); %>
<asp:dropdownlist id="STATE"
DataSource="<%# DataSet_DistinctState.DefaultView %>"
DataTextField="STATE"
DataValueField="STATE"
runat="server">
<asp:ListItem Value="">Please select an option</asp:ListItem>
</asp:dropdownlist>
it might work.
Alternatively, if that doesnt work, then add this to the code behind portion of your page:
STATE.Items.Add("Please Select"
or you could try making a post page loading javascript to add an option at the top of the select list,
or if all else fails, change the code from an asp:droplist to a asp:repeater and format the output into option tags, and add the static one at the top.
Replied 03 Jul 2008 10:02:32
03 Jul 2008 10:02:32 Rajeev Lal replied:
STATE.Items.Add("Please Select" will not work. Use STATE.Items.Insert(0, "Please Select" where first one is the index to insert and second parameter is the string value to insert at that index.
Replied 25 Mar 2012 18:16:21
25 Mar 2012 18:16:21 vayane nick replied:
try like this
ListItem[] items = new ListItem[3];
items[0] = new ListItem("One", "1"
items[1] = new ListItem("Two", "2"
items[2] = new ListItem("Three", "3"
DropDownList1.Items.AddRange(items);
DropDownList1.DataBind();
csharp.net-informations.com
vayne nick.
ListItem[] items = new ListItem[3];
items[0] = new ListItem("One", "1"
items[1] = new ListItem("Two", "2"
items[2] = new ListItem("Three", "3"
DropDownList1.Items.AddRange(items);
DropDownList1.DataBind();
csharp.net-informations.com
vayne nick.