0
我有兩個下拉列表;第一個綁定到實體數據源,第二個根據用戶從第一個列表中選擇的內容而變化(最初它也是基於第一個下拉列表預先填充的)。由於某種原因,第二個列表中的數據沒有被保存到數據庫,我找不到原因。數據沒有綁定到更新下拉列表
這裏是我的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>
以下是我在代碼中設置了後面:
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?它完全忽略了用戶在該下拉列表中選擇的任何值。它正確綁定(一切正常顯示),但似乎沒有提交與formview的其餘部分。
催眠..由代碼牆..儘管如此,嘗試簡化形式。一次去除一件,直到它開始工作。然後,你知道你改變的最後一件事是什麼導致你的問題。 –
它工作正常,除了無論用戶從第二個下拉列表中選擇什麼都不保存到數據庫,它保持它原來的默認值。 – Omni