rondre
asked on
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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&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;
}
}
}
om_prakash_p is right - sorry I didn't see you had a ListItem already there!
ASKER
thank you - that works perfect!
ASKER