Prepend and Append Dropdownlist

Posted on 2011-10-03
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

Question by:Robert Treadwell
    1 Comment
    LVL 17

    Accepted Solution

    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>

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    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 (…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now