• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

Prepend and Append Dropdownlist

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
Robert Treadwell
Asked:
Robert Treadwell
1 Solution
 
Carlos VillegasFull Stack .NET DeveloperCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now