How to Set the Default Value of a DropDownList in an ASP.NET Page
Wednesday 17 October 2007 @ 10:44 am

How to Set the Default Value of a DropDownList in an ASP.NET Page

(Using Membership ObjectDataSource From MSDN Article)

I'm doing a project where I have lots of DropDownList's on a page and want a simple way to set the initial values based on some known value.  The DropDownLists are retrieved using ObjectDataSource but I don't want the first value displayed.  I wrestled some with how to do this using page_load or page_prerender but didn't come up with good solutions.  I did finally decide that programming the databound event of the dropdownlist was probably the best way to go. 

The solution is pasted below.  The MembershipUtilities.MembershipODS c# code can be downloaded from MSDN or on my web site at the following location.  http://peterkellner.net/2006/01/09/microsoft-aspnet-20-memberrole-management-with-iis/.

 

<%@ Page Language=”C#” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

  “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<script runat=”server”>

 

    protected void DropDownListUser_DataBound(object sender, EventArgs e)

    {

        DropDownListUser.SelectedIndex =

          DropDownListUser.Items.IndexOf

          (DropDownListUser.Items.FindByValue(Context.User.Identity.Name));

    }

</script>

 

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head runat=”server”>

    <title>DropDownList Initialize</title>

</head>

<body>

    <form id=”form1″ runat=”server”>

    <div>

        <asp:DropDownList ID=”DropDownListUser” runat=”server”

          DataSourceID=”ObjectDataSourceUser”

          DataTextField=”UserName” DataValueField=”UserName”

          OnDataBound=”DropDownListUser_DataBound”>

        </asp:DropDownList>

        <asp:ObjectDataSource ID=”ObjectDataSourceUser” runat=”server”

          SelectMethod=”GetMembers”

          TypeName=”MembershipUtilities.MembershipUserODS”>

            <SelectParameters>

                <asp:Parameter Name=”sortData” Type=”String” />

            </SelectParameters>

        </asp:ObjectDataSource>

    </div>

    </form>

</body>

</html>





4 Responses to “How to Set the Default Value of a DropDownList in an ASP.NET Page”

  1. George Windows XP Mozilla Firefox 2.0.0.10 Says:

    Why you don’t use just

    DropDownListUser.SelectedValue = Context.User.Identity.Name;

    instead of

    DropDownListUser.SelectedIndex =
    DropDownListUser.Items.IndexOf(
    DropDownListUser.Items.FindByValue(Context.User.Identity.Name));

    ?

  2. Naveen J Windows XP Mozilla Firefox 2.0.0.11 Says:

    I dont agree with george, since it replaces the selectedvalue.
    But Peter
    why cant we use
    DropDownListUser.Items.FindByValue(Context.User.Identity.Name).Selected = true;
    is it not more simpler?

  3. mrnon Windows XP Internet Explorer 7.0 Says:

    I agree with NaveenJ. His approach is just want i’m using :p

  4. jbfurlong Windows Vista Internet Explorer 7.0 Says:

    I use Naveen’s approach too but at run time when databind occurs, if the SelectedIndex is not cleared to -1 first then you can get an http error that you cannot have more than one item selected. It goes something like
    DropDownList.SelectIndex = -1;
    DropDownListUser.Items.FindByValue(Context.User.Identity.Name).Selected = true;

Leave a Reply