C# 为什么Telerik Radgrid有时能工作

C# 为什么Telerik Radgrid有时能工作,c#,asp.net,telerik,telerik-grid,C#,Asp.net,Telerik,Telerik Grid,我有一个不寻常的情况,我似乎无法解决。我有一个Telerik RadGrid设置,可以使用.ascx web用户控件编辑记录和添加新记录。表单有1个ASP.net DropdownList,在编辑记录时可以正常工作。但是,当我尝试添加记录时,应用程序会崩溃,并显示以下错误消息: “DropDownList 1”有一个SelectedValue,该值无效,因为它不在项目列表中。 参数名称:值说明:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。

我有一个不寻常的情况,我似乎无法解决。我有一个Telerik RadGrid设置,可以使用.ascx web用户控件编辑记录和添加新记录。表单有1个ASP.net DropdownList,在编辑记录时可以正常工作。但是,当我尝试添加记录时,应用程序会崩溃,并显示以下错误消息:

“DropDownList 1”有一个SelectedValue,该值无效,因为它不在项目列表中。 参数名称:值说明:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.ArgumentOutOfRangeException:“DropDownList1”有一个SelectedValue,该值无效,因为它不在项目列表中。 参数名称:value

下面是用于在.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>
<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>

有人能解释一下为什么表单在编辑时不会出错,而在添加记录时不会出错吗?还有关于如何修复它的任何建议。我尝试过清理和重建解决方案,但没有帮助

谢谢
佩里

我找到了这个问题的解决办法。我在下拉列表中缺少一个设置。更正后的代码如下所示:

<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>
<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”

代码现在用于添加和编辑记录