How do I populate a dropdownlist in detailsview

Posted on 2009-04-21
Medium Priority
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
  • 2
  • 2

Expert Comment

ID: 24196884
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

ID: 24197812
Where in the code would I add this?? At page load????

Expert Comment

ID: 24201437
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

ID: 24205459
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>

Accepted Solution

AxleWack earned 1500 total points
ID: 24352207

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 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