Append Blank Item to Databound Dropdown

Posted on 2007-10-18
Last Modified: 2008-01-09
I am looking to add a blank default item to a dropdown list in an form.
I can not add the item directly to the database in this situation.

I have tried the following code for the DropdownList however the item did not appear:

 <asp:DropDownList ID="C_DepartmentID" runat="server" AutoPostBack="True" DataSourceID="D_DEPTs"
                            DataTextField="Name" DataValueField="ID" Width="16em">
                                <asp:ListItem Value="0" Selected="true">Select Department</asp:ListItem>

I Then set the AppendDataBoundItems="True"  
This does make the blank item appear in the list, however the side effect is that if the conditions populating the list change- the list items from the new parameters are appended to the old and both can be selected. This can obviously cause some big problems.

 Is there a fix for this side effect, or perhaps another method of adding this blank default ?
As always thank you for your help!

Question by:apocalypse910
    LVL 4

    Assisted Solution

    In instances such as the one you present I use a UNION statement in my SQL to add an aditional SELECT statment with hard coded values that provide the column you need, for example
    SELECT 'Select Department' as Name, 0 as Value
    SELECT Name, Value FROM Table

    I'm not sure if this would contravean your statment about not adding to the database though?
    LVL 26

    Accepted Solution

    in page load

    if not ispostback() then
      DepartmentID.Items.Insert(0, "Select department")
    end if


    Author Comment

    I sincerely apologize for the delay in getting back to you. i got involved in another project and hadn't had a chance to work on this issue for a bit. Thank you very much for your time and the excellent solutions.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    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…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 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