Hello,
I posted a question earlier but have another problem after making those changes. The previous thread can be found here:
http://stackoverflow.com/questions/2700028/binding-a-dropdownlist-inside-a-detailsview
Basically, I've got a dropdownlist that's dynamically populated with a list of regions. It selects the correct region when viewing the dropdown, but when I try to edit it changes the value to null. I think it might be because it doesn't know which field to update. Previously, when the dropdown list was hardcoded, I had SelectedValue='<%# Bind("region_id")%' set on the dropdown list, and when I updated it worked fine. However, I had to move the setting of the selected value into the code behind and now it just gets set to null every time I update.
Here's the aspx code:
<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc" DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" OnDataBound="DetailsView1_DataBound" >
<Fields>
<snip>
<asp:TemplateField HeaderText="Region">
<ItemTemplate><%# Eval("region_name") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="RegionDropdownList" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
And here's the code behind:
protected void DetailsView1_DataBound(object sender, EventArgs e)
{
ArrayList regionsList = BPBusiness.getRegions();
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
if (ddlRegions != null)
{
ddlRegions.DataSource = regionsList;
ddlRegions.DataValueField = "Value";
ddlRegions.DataTextField = "Text";
ddlRegions.DataBind();
if (ddlRegions.Items.Contains(ddlRegions.Items.FindByValue(objBusiness.iRegionID.ToString())))
{
ddlRegions.SelectedIndex = ddlRegions.Items.IndexOf(ddlRegions.Items.FindByValue(objBusiness.iRegionID.ToString()));
}
}
}
}
EDIT:
The database is MySql, and the update statement looks like this:
myMySqlDataSrc.UpdateCommand = "UPDATE myTable SET business_name = ?, addr_line_1 = ?, addr_line_2 = ?, addr_line_3 = ?, postcode = ?, county = ?, town_city = ?, tl_url = ?, customer_id = ?, region_id = ?, description = ?, approval_status = ?, tl_user_name = ?, phone = ?, uploaders_own = ? WHERE id = ?";
Thanks,
Annelie