I am having trouble trying to populate a drop down list from a database. When I try to set the data source, I cannot find the drop-down control, it is in DetailsView, so I think that it can have something to do with it only when it is created in edit mode. He still talks about it in the current mode when I edit, so I donβt know what is going on there.
Here is the code from the aspx file:
<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc" DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" >
<Fields>
<asp:TemplateField HeaderText="Region">
<ItemTemplate><%# Eval("region_name") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>' />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And this is from the code behind:
ArrayList regionsList = BPBusiness.getRegions();
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
if (ddlRegions != null)
{
ddlRegions.DataSource = regionsList;
ddlRegions.DataBind();
}
}
source
share