Solved

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

Posted on 2010-08-20
5
478 Views
Last Modified: 2012-05-10
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
Comment
Question by:rondre
  • 2
  • 2
5 Comments
 

Author Comment

by:rondre
ID: 33483473
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
 
LVL 22

Accepted Solution

by:
Om Prakash earned 500 total points
ID: 33483618
Add AppendDataBoundItems="true" to asp:DropDownList

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sdsMember" DataTextField="MEMBER_SNAME" DataValueField="MEMBER_ID" AppendDataBoundItems="true">
0
 
LVL 19

Expert Comment

by:daveamour
ID: 33483655
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
 
LVL 19

Expert Comment

by:daveamour
ID: 33483734
om_prakash_p is right - sorry I didn't see you had a ListItem already there!
0
 

Author Closing Comment

by:rondre
ID: 33486149
thank you - that works perfect!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

778 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