我有两个下拉列表;第一个绑定到实体数据源,第二个根据用户从第一个列表中选择的内容而变化(最初它也是基于第一个下拉列表预先填充的)。由于某种原因,第二个列表中的数据没有保存到数据库中,我不知道为什么。
这是我的表单视图:
<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>
这是我在后面的代码中设置的内容:
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"));
}
为什么除了 DropDownList2 之外的所有内容都正确保存?它完全忽略了用户在该下拉列表中选择的任何值。它绑定正确(一切显示正确),但似乎没有与表单视图的其余部分一起提交。