Binding a DropDownList inside a DetailsView
Posted
by annelie
on Stack Overflow
See other posts from Stack Overflow
or by annelie
Published on 2010-04-23T15:58:10Z
Indexed on
2010/04/23
16:13 UTC
Read the original article
Hit count: 431
Hello,
I'm having problems trying to populate a dropdownlist from the database. When I'm trying to set the datasource I can't find the dropdown control, it's in a DetailsView so I think it might have something to do with it only being created when it's in edit mode. It still says it's in current mode when I'm editing though, so not sure what's going on there.
Here's 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>
<snip>
<asp:TemplateField HeaderText="Region">
<ItemTemplate><%# Eval("region_name") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>'>
</asp:DropDownList>
</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();
}
}
It's .net 2.0 if that helps.
Thanks,
Annelie
© Stack Overflow or respective owner