How do I populate a dropdownlist in detailsview

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?????????
mmtcunningham1Asked:
Who is Participating?
 
AxleWackConnect With a Mentor Commented:
Hi,

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



0
 
VikramMullickCommented:
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"]))
}
0
 
mmtcunningham1Author Commented:
Where in the code would I add this?? At page load????
0
 
VikramMullickCommented:
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.
0
 
mmtcunningham1Author Commented:
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">
                        <EditItemTemplate>
                            &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>
                            </asp:DropDownList>
                        </EditItemTemplate>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.