'ddlUser'有一個SelectedValue,它是無效的,因爲它不存在於項目列表中 。參數名:價值防止不存在項目的綁定
這是ERRORMESSAGE我得到的,當我打電話以下代碼:
ASPX:
<asp:FormView ID="FormView1" runat="server" DataSourceID="EntityDataSource1">
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlUser" Selectedvalue='<%# Bind("UserID") %>'
AppendDataBoundItems="true" DataSourceID="EntityDataSource2"
DataTextField="UserName" DataValueField="UserID" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="EntityDataSource2" runat="server" EntitySetName="User" AutoGenerateWhereClause="true">
<WhereParameters>
<asp:Parameter Name="deleted" DefaultValue="false" Type="Boolean" />
</WhereParameters>
</asp:EntityDataSource>
這樣做的原因問題是,那EntityDataSource2
只選不是已刪除用戶。但選定的用戶可以隨時刪除,然後他們不再出現在結果中,這會導致此錯誤。
我已經搜索了答案,只喜歡this之一,它的工作原理,但產生重複的條目(因爲DataBind
被稱爲兩次)。另一個討論here沒有結果。
問題:如何防止此錯誤,並選擇空的項目,如果用戶在此期間被刪除。
我試過到目前爲止:
首先我想到的
protected void DropDownList1_DataBinding(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)sender;
if(!Helper.CheckIfValid(DropDownList1.SelectedValue))
{
DropDownList1.SelectedValue = "";
}
}
但SelectedValue
沒有設置在DataBinding
事件。
變化'DropDownList1.SelectedValue = 「」''到= DropDownList1.SelectedIndex -1' – levent