为什么Telerik Radgrid会在某些时候工作

时间:2016-03-23 14:23:49

标签: c# asp.net telerik telerik-grid

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

  

' DropDownList1'具有一个无效的SelectedValue,因为它不存在于项列表中。    参数名称:value描述:执行当前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>

任何人都可以解释为什么表单会在没有错误的情况下呈现为编辑而不是记录添加?以及我如何解决它的任何建议。我尝试过清理和重建解决方案,但这没有用。

由于 佩里

1 个答案:

答案 0 :(得分: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 =&#34; true&#34;。

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