?
Solved

Prepend and Append Dropdownlist

Posted on 2011-10-03
1
Medium Priority
?
479 Views
Last Modified: 2012-06-21
I'm trying to prepend and append values to DropdownList.  Currently all dropdownlist contain ListItem with Text equal to '- Select -"  and all other database items appended dynamically to control.  Customer now wants to append 'Other' to every list.  I do not want to place in table because I want 'Other' to always be the last item in the list.  How do I do this?

See example control in code section.
<asp:DropDownList ID="Allergies1DDL" runat="server" AppendDataBoundItems="True" 
                                                   DataSourceID="AllergiesDS" DataTextField="Allergy" 
                                                   DataValueField="Allergy" TabIndex="1" Width="12.5em">
                                                   <asp:ListItem Text="-Select-" Value="0"></asp:ListItem></asp:DropDownList>

Open in new window

0
Comment
Question by:Robert Treadwell
1 Comment
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 36906044
Hello, use your DropDownList DataBound event:

Your aspx page code:
<asp:DropDownList ID="Allergies1DDL" runat="server" AppendDataBoundItems="True" DataSourceID="AllergiesDS"
    DataTextField="Allergy" DataValueField="Allergy" TabIndex="1" Width="12.5em" OnDataBound="Allergies1DDL_DataBound">
    <asp:ListItem Text="-Select-" Value="0"></asp:ListItem>
</asp:DropDownList>

Open in new window


Your code behind file:
protected void Allergies1DDL_DataBound(object sender, EventArgs e)
{
    Allergies1DDL.Items.Add(new ListItem("Other", "-1"));
}

Open in new window


You must change the -1 value to a proper value for you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question