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

Add non sqldatasource listitems to asp:dropdown linked to a sqldatasource

I have an asp:dropdown populated from a sqldatasource control and it works great.  However, I need to add 1 more listitem that is not pulled from the query to put in the dropdown but it does not show up.  How can I do this?
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsMember" DataTextField="MEMBER_SNAME" DataValueField="MEMBER_ID">
                                    
<asp:ListItem Text="All Members" Value="0" Selected="True"></asp:ListItem>
                                    
</asp:DropDownList>
                                    
<asp:SqlDataSource ID="sdsMember" runat="server" ConnectionString="<%$ ConnectionStrings:DBSFL %>" SelectCommand="select MEMBER_SNAME, MEMBER_ID from MEMBER order by MEMBER_SNAME ASC"></asp:SqlDataSource>

Open in new window

0
rondre
Asked:
rondre
  • 2
  • 2
1 Solution
 
rondreAuthor Commented:
In case it wasn't clear, the output on the page is all of the items from the select statement of the sqldatasource but the asp:listitem "All Members" does not show up - looking to see how this is done.  Thanks!
0
 
Om PrakashCommented:
Add AppendDataBoundItems="true" to asp:DropDownList

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsMember" DataTextField="MEMBER_SNAME" DataValueField="MEMBER_ID" AppendDataBoundItems="true">
0
 
daveamourCommented:
Check it out....
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="EETest._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>

    <asp:DropDownList ID="People" runat="server"></asp:DropDownList>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EETest
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            People.DataSource = GetPeople();
            People.DataBind();

            People.PreRender += new EventHandler(People_PreRender);
        }

        private void People_PreRender(object sender, EventArgs e)
        {
            People.Items.Insert(0, new ListItem("Sharon"));
        }

        private List<String> GetPeople()
        {
            List<String> people = new List<string>();

            people.Add("Dave");
            people.Add("Fred");
            people.Add("Sarah");

            return people;
        }
    }
}

Open in new window

0
 
daveamourCommented:
om_prakash_p is right - sorry I didn't see you had a ListItem already there!
0
 
rondreAuthor Commented:
thank you - that works perfect!
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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