2016-03-23 82 views
-1

我有一個不尋常的情況,我似乎無法解決。我有一個Telerik RadGrid設置使用.ascx網頁用戶控件編輯記錄並添加新記錄。當我編輯記錄時,表單有1個ASP.net DropdownList。但是,當我嘗試添加記錄時,應用程序崩潰並顯示以下錯誤消息:爲什麼這個Telerik Radgrid在某些時候工作

'DropDownList1'具有一個無效的SelectedValue,因爲它在項目列表中不存在。 參數名稱:值說明:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。
異常詳細信息:System.ArgumentOutOfRangeException:'DropDownList1'具有一個無效的SelectedValue,因爲它不在項目列表中。 參數名稱:值

以下是用於構建.ascx頁面下拉列表的代碼。

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource1" 
        DataTextField="StatusDescription" DataValueField="StatusDescription" 
        SelectedValue='<%# DataBinder.Eval(Container, "DataItem.Status") %>'> 
        <asp:ListItem Selected="True" Text="Select" Value=""> 
         </asp:ListItem> 
       </asp:DropDownList> 

的是使用EntityDataSource和代碼下拉數據源如下所示:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=TipsFileEntities1" DefaultContainerName="TipsFileEntities1" EntitySetName="Status" 
        Select="it.[StatusDescription], it.[StatusCode]" AutoPage="true" OrderBy="it.[StatusDescription]"> 
       </asp:EntityDataSource> 

任何人都可以解釋爲什麼會形式沒有錯誤渲染編輯而不是一個記錄添加?以及有關如何修復它的任何建議。我曾嘗試清理並重建解決方案,但這並沒有幫助。

感謝 佩裏

回答

0

我能夠追查解決這個問題。我錯過了DropDownList中的設置。正確的代碼如下所示:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource1" 
        DataTextField="StatusDescription" DataValueField="StatusDescription" AppendDataBoundItems ="true" 
        SelectedValue='<%# DataBinder.Eval(Container, "DataItem.Status") %>'> 
        <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem> 
       </asp:DropDownList> 

我已經遺漏了AppendDataBoundItems =「true」。

現在可以添加和編輯記錄的代碼

相關問題