2017-03-20 73 views
1

'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事件。

+0

變化'DropDownList1.SelectedValue = 「」''到= DropDownList1.SelectedIndex -1' – levent

回答

1

我建議你定義在你的項目的自定義控件(和引用它,而不是asp:DropDownList)從標準DrowDownList衍生不會拋出時該選擇未被定義爲列表中的項目。

如果應該按原樣工作(默認情況下,將選擇空值項目),但您也可以使用其某個屬性定義您認爲是代表「未選定」項目的值或文本。這裏是類:

public class ExtendedDropDownList : System.Web.UI.WebControls.DropDownList 
{ 
    protected override void PerformDataBinding(IEnumerable dataSource) 
    { 
     try 
     { 
      base.PerformDataBinding(dataSource); 
     } 
     catch (ArgumentOutOfRangeException) 
     { 
      ListItem item; 

      // try the value we defined as the one that represents <no selection> 
      if (NoSelectionValue != null) 
      { 
       item = Items.FindByValue(NoSelectionValue); 
       if (item != null) 
       { 
        item.Selected = true; 
        return; 
       } 
      } 

      // try the text we defined as the one that represents <no selection> 
      if (NoSelectionText != null) 
      { 
       item = Items.FindByText(NoSelectionText); 
       if (item != null) 
       { 
        item.Selected = true; 
        return; 
       } 
      } 

      // try the empty value if it exists 
      item = Items.FindByValue(string.Empty); 
      if (item != null) 
      { 
       item.Selected = true; 
       return; 
      } 
     } 
    } 

    [DefaultValue(null)] 
    public string NoSelectionValue 
    { 
     get 
     { 
      return (string)ViewState[nameof(NoSelectionValue)]; 
     } 
     set 
     { 
      ViewState[nameof(NoSelectionValue)] = value; 
     } 
    } 

    [DefaultValue(null)] 
    public string NoSelectionText 
    { 
     get 
     { 
      return (string)ViewState[nameof(NoSelectionText)]; 
     } 
     set 
     { 
      ViewState[nameof(NoSelectionText)] = value; 
     } 
    } 
} 

}

+0

適合我! – Toshi

0

這是一項圍繞建議的工作。我認爲這不是一個乾淨的解決方案,但希望它可以幫助。

此問題的原因是所選項目不存在於項目列表中。爲了防止它發生,我們應該確保所選項目將始終存在,方法如下。

我們添加一個與選定值具有相同值的項目。

<asp:DropDownList runat="server" ID="ddlUser" Selectedvalue='<%# Bind("UserID") %>' 
         AppendDataBoundItems="true" DataSourceID="EntityDataSource2" 
         DataTextField="UserName" DataValueField="UserID" 
         OnDataBound="OnListDataBound" >   
      <asp:ListItem Value="">(none)</asp:ListItem> 
      <asp:ListItem Value='<%# Bind("UserID") %>'></asp:ListItem> 
</asp:DropDownList> 

然後在數據綁定事件,我們除了做檢查,以找出weither選擇的值是否有效。

  • 如果只有一個與所選值具有相同值的列表項目, 它應該是無效的選定值。我們應該從列表 中刪除它,並將選定的值重置爲空。
  • 如果有兩個項目與選定的值具有相同的值,我們應該刪除空白文本的項目並將選定的值設置爲剩餘項目。
protected void OnListDataBound(object sender, EventArgs e) 
{ 
    // addition check 
} 

UPDATE
修正問題後,似乎我一直在想太複雜了。我上面的建議也是一個不好的解決方案。
顯然,選擇列表中不存在的項目是無效的。因此,試圖綁定一個可能無效的值,爲什麼我們不移除它,然後在後面的代碼中選擇正確的值?

ASPX

<asp:DropDownList runat="server" ID="ddlUser" 
    AppendDataBoundItems="true" DataSourceID="EntityDataSource2" 
    DataTextField="UserName" DataValueField="UserID" 
    OnDataBound="OnListDataBound" >   
      <asp:ListItem Value="">(none)</asp:ListItem> 
</asp:DropDownList> 

代碼隱藏

protected void OnListDataBound(object sender, EventArgs e) 
{ 
    DropDownList dropdown = (DropDownList)sender; 
    string userId = string.Empty; 

    // Assign value for userId from FormView1.DataSource 
    .... 

    // Check to select valid item 
    ListItem foundItem = dropdown.Items.FindByValue(userId); 
    if (foundItem != null) 
    { 
     dropdown.SelectedValue = userId; 
    } 
    else 
    { 
     dropdown.SelectedValue = string.Empty; 
    } 
} 
+0

在FormView控件插入模式 – Toshi

+0

不工作我已經更新了我回答,請檢查 –

+0

在OnListDataBound'之前拋出異常,所以不調用此方法 – Toshi