2012-11-15 12 views
2

我有一個綁定到LINQ數據源的下拉列表。該下拉列表顯示除數據庫中的whos狀態設置爲false以外的所有bowzer編號。
假設我有一個更早創建的記錄,現在我想編輯現在設置爲false的bowzer。我被拋出這個例外,我不知道如何處理它。Linq布爾返回異常DROPDOWNLIST有一個無效的選擇值,因爲它不存在於項目列表中

'bowzerddl'有一個SelectedValue,它是無效的,因爲它不存在於項目列表中。 參數名:價值

這裏是我的LinqDataSource

<asp:LinqDataSource ID="LinqBowzerDS" runat="server" ContextTypeName="ShippingWeb.ShippingDbDataContext" 
EntityTypeName="" Select="new (bowzer_id, bowzer_no)" TableName="Bowzers" 
OrderBy="bowzer_no" Where="expiry_date &gt;= DateTime.Now && status==True"> 

代碼當我點擊DetailsView控件,在我的下拉列表是編輯按鈕,我給出一個例外,我」前面已經提到過。有什麼辦法可以捕捉到這個異常?並在編輯時,只顯示那些狀態爲True的記錄?

這裏是我的DetailsView

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" 
      DataSourceID="LinqDataSource1" Height="50px" 

      Width="363px" 
      DataKeyNames="challan_id" ForeColor="#333333" GridLines="None" 
      ondatabound="DetailsView1_DataBound" onmodechanged="DetailsView1_ModeChanged" 
     > 

<asp:TemplateField HeaderText="Bowzer No"> 
     <EditItemTemplate> 
      <asp:DropDownList ID ="bowzerddl" runat="server" 
      DataSourceID="LinqBowzerDS" 
      DataTextField="bowzer_no" 
      DataValueField="bowzer_id" 
      selectedValue='<%# bind("bowzer_id") %>' 
      AppendDataBoundItems="true" 
      > 
      <asp:ListItem Selected="True" Value="">(None)</asp:ListItem> 
      </asp:DropDownList> 

     </EditItemTemplate> 
     <ItemTemplate> 
      <asp:Label ID="lblbowzer" runat="server" Text='<%# Eval("Bowzer.Bowzer_no") %>'>                                  </asp:Label> 
     </ItemTemplate> 

    </asp:TemplateField> 

    <asp:CommandField ShowEditButton="True" /> 

</Fields> 
<FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" /> 
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
<PagerStyle ForeColor="White" HorizontalAlign="Center" BackColor="#284775" /> 
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
</asp:DetailsView> 

回答

2

我解決了它從這個鏈接Answer No 16

這個工作對我們的代碼:

protected void PreventErrorOnbinding(object sender, EventArgs e) 
    { 
     dropdownlist theDropDownList = (DropDownList)sender; 
     theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding); 
     theDropDownList.AppendDataBoundItems = true; 
     ListItem li = new ListItem("Make a selection >>",""); 
     theDropDownList.Items.Insert(0, li); 
     try 
     { 
      theDropDownList.DataBind(); 
     } 
     catch (ArgumentOutOfRangeException) 
     { 
      theDropDownList.SelectedValue = ""; 
     } 
    } 

在添加ondatabinding = 「PreventErrorOnbinding」

由於所選擇的值的控制(一個或多個)被設置爲 「」,它總是在下拉列表選擇firts 項。

相關問題