I have two drop down lists; the first is bound to an entity datasource, and the second changes depending on what the user selected from the first list (Initially it is pre-populated based on the first drop down as well). For some reason the data from the second list is not being saved to the database and I can’t figure out why.
Here is my formview:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="SuggestionID" OnItemCommand="FormView1_ItemCommand" OnItemUpdated="FormView1_ItemUpdated"
DataSourceID="UpdateSuggestionEntity" DefaultMode="Edit" OnDataBound="FormView1_Databound" OnItemUpdating="Formview1_OnItemUpdating">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
DataSourceID="ServiceAreaEntity" DataTextField="ServiceAreaConcatenated" AutoPostBack="true"
DataValueField="ServiceAreaID" SelectedValue='<%# Bind("ServiceAreaID") %>'>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" OnPreRender="AddEmptySelect">
</asp:DropDownList>
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:Button ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:FormView>
Here is what I have set up in my code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities context = new DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities();
DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
int ID = Int32.Parse(Request.QueryString["SuggestionID"]);
var suggestion = from u in context.Suggestions
where u.SuggestionID == ID
select u;
foreach (var i in suggestion) {
var subServiceArea = from u in context.SubServiceAreas
where u.ServiceAreaID == i.ServiceAreaID
select u;
DropDownList2.DataSource = subServiceArea;
DropDownList2.DataTextField = "SubServiceAreaName";
DropDownList2.DataValueField = "SubServiceAreaID";
DropDownList2.DataBind();
DropDownList2.SelectedValue = i.SubServiceAreaID.ToString();
}
}
}
// Dynamically populate the SubService Area Dropdown
protected void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e) {
DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities context = new DigitalSuggestionBox.Data_Access.DigitalSuggestionBox_Entities();
DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1") as DropDownList;
DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
int valueSelected = Int32.Parse(DropDownList1.SelectedItem.Value);
var subServiceArea = from u in context.SubServiceAreas
where u.ServiceAreaID == valueSelected
select u;
DropDownList2.DataSource = subServiceArea;
DropDownList2.DataValueField = "SubServiceAreaID";
DropDownList2.DataTextField = "SubServiceAreaName";
DropDownList2.DataBind();
}
protected void AddEmptySelect(Object sender, EventArgs e) {
DropDownList DropDownList2 = (DropDownList)FormView1.FindControl("DropDownList2") as DropDownList;
DropDownList2.Items.Insert(0, new ListItem("- Select -", "0"));
}
Why does everything else get saved correctly except for DropDownList2? It completely ignores whatever value the user selects in that drop down. It’s bound correctly (everything appears correctly) but doesn’t seem to get submitted with the rest of the formview.
I kept digging around and found this article:
http://www.codeproject.com/Questions/191898/DropDownList-and-capturing-values-after-SelectedIn
I feel like there is a better way to do this, but I haven’t figured any out and this method works like a charm. If anyone would like to contribute I’d be interested to see what other solutions are possible.