【问题标题】:'detailsUserTitle' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value'detailsUserTitle' 有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值
【发布时间】:2014-09-09 15:31:57
【问题描述】:

DropDownList 不断返回此错误 :(

这是我的 aspx 代码

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="username" DataSourceID="SqlDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
    <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <Fields>
        <asp:TemplateField HeaderText="username" SortExpression="username">
            <ItemTemplate>
                <asp:Label ID="Label9" runat="server" Text='<%# Bind("username") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Title" SortExpression="title">
            <EditItemTemplate>
                <asp:DropDownList ID="detailsUserTitle" runat="server" SelectedValue='<%# Bind("title") %>' DataSourceID="SqlDataSource1">
                    <asp:ListItem Value="-1">Select Title</asp:ListItem>
                    <asp:ListItem Value="Mr">Mr</asp:ListItem>
                    <asp:ListItem Value="Mrs">Mrs</asp:ListItem>
                    <asp:ListItem Value="Miss">Miss</asp:ListItem>
                    <asp:ListItem Value="Ms">Ms</asp:ListItem>
                </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorTitle" 
                    ControlToValidate="detailsUserTitle" runat="server" ErrorMessage="Title is a required field" 
                    Text="*" ForeColor="Red" InitialValue="-1"></asp:RequiredFieldValidator>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
            </ItemTemplate>

我一直在网上寻找建议,但没有一个对我有帮助的结论性建议。请帮忙。我没有在后面添加任何与此问题相关的代码。

谢谢

【问题讨论】:

    标签: c# asp.net drop-down-menu


    【解决方案1】:

    您的问题在于 DDL 属性 SelectedValue='&lt;%# Bind("title") %&gt;'

    您的 SQL 数据源“SqlDataSource1”可能不会返回来自 Mr、Mrs、Miss 或 MS 的任何值。

    如果 Title 字段的值不同,则会出现此错误

    更新

    您可以在绑定之前验证您的标题字符串,如下所示。但是,我不会推荐这个。如果您可以在从数据源检索之前验证值,我更喜欢。理想的解决方案是将 DropDownList 与相同的值集绑定,然后再找到其中的值。那么你不会错过任何东西。

    这里是快速修复。 :-)

    SelectedValue='<%# (!string.IsNullOrEmpty(Bind("title")) || "Mr, Mrs, Miss, MS".IndexOf(Bind("title")) > -1) ? Bind("title") : "-1" %>'
    

    更新 2 给你:-D

    SelectedValue='<%# (!string.IsNullOrEmpty((string)Eval("title")) || "Mr, Mrs, Miss, MS".IndexOf((string)Eval("title")) > -1) ? (string)Eval("title") : "-1" %>'
    

    如果它解决了您的问题,请接受答案。万事如意!

    【讨论】:

    • SQL 数据源确实具有来自不同记录的 Mr、Mrs、Miss 和 Ms 值。也许我应该做一个绑定检查?但是我不知道在这种情况下该怎么做如何检查 Title 是否具有不同的值?
    • 嗨 Sam,感谢您的快速修复。但是我从那个快速修复中得到一个编译错误它说:描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。编译器错误消息:CS0103:名称“绑定”在当前上下文中不存在
    • 嗯,你可以试试用 Eval("title") 代替 Bind("title") 吗?
    • 嘿山姆,我可以将 Eval 添加到 SelectedValue 的唯一方法是,如果我将它放在最后一个 Bind 中,就像这样 SelectedValue=' -1) ? Eval("title") : "-1" %>' 我收到相同的编译错误 ~ 谢谢 :)
    • 什么意思?您应该能够用 Eval 替换所有 Bind 语句
    猜你喜欢
    • 2015-11-02
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多