How do I populate a dropdownlist in detailsview

Posted on 2009-04-21
Last Modified: 2013-11-26
I have a dropdownlist in my details view.  I currently have it set up to be populated via a sql database file; however, I would like to add an additional item(a string) to the top of the dropdownlist in addition to the items in the database file.
How can I get this populated?????????
Question by:mmtcunningham1
    LVL 9

    Expert Comment

    ddllist.items.add(new listitem("new value","testvalue"));
    datatable dt;
    //dt conatins data feteched from the database
    for(int i = 0;i<dt.rows.count;i++)
    ddllitst.items.add(new listitem(dt.rows[i]["abc"],dt.rows[i]["def"]))

    Author Comment

    Where in the code would I add this?? At page load????
    LVL 9

    Expert Comment

    This all depends on which event you want to enter the values.
    If you want the values to be populated on Page load , then yes it has to be on Page load.

    Author Comment

    Just putting in the name of the dropdownlist does not work.  It doesn't recognize it.  Here is the asp code and I'm trying to populate the ddlAssignerName which is a template field within the detailsview1
    <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
                    CellPadding="4" DataKeyNames="CommunicationID" DataSourceID="dsActionBranches"
                    ForeColor="#333333" GridLines="None" Height="50px" Width="738px">
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <asp:TemplateField HeaderText="ActionAssignerName" SortExpression="ActionAssignerName">
                                &nbsp;<asp:DropDownList ID="ddlAssignerName" runat="server" AppendDataBoundItems="True"
                                    DataSourceID="dsUsers" DataTextField="FirstNameLastName" DataValueField="FirstNameLastName"
                                    SelectedValue='<%# Bind("ActionAssignerName") %>' Width="151px">
                                    <asp:ListItem Selected="True"></asp:ListItem>
    LVL 3

    Accepted Solution


    In your gridview, first set your field to a template, then assuming you have placed a dropdownlist in the area you want it in the details view, you use the following code to be read in your code:

    Assuming your DropDownlist is called DropDownList1,

    Dim DropDownlist1 As DropDownList = DetailsView1.FindControl("DropDownlist1")

    you can now populate this gridview that is in your details view, in its Databinding event. Example.

     Protected Sub ddlContract_Period_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
          Dim DropDownlist1 As DropDownList = DetailsView1.FindControl("DropDownlist1")
          DropDownList1.SelectedText = "Hello World"
        End Sub

    Hope this helps


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now