'detailsUserTitle'具有的SelectedValue,因为它不在项目列表中存在这是无效的。参数名:价值 [英] 'detailsUserTitle' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value

查看:207
本文介绍了'detailsUserTitle'具有的SelectedValue,因为它不在项目列表中存在这是无效的。参数名:价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DropDownList的不断返回该错误:(

DropDownList keeps returning this error :(

下面是我的aspx code

Here is my aspx code

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

我一直在寻找网上的意见,但一直没有确凿的一个帮助我。请帮忙。我没有相关的背后添加任何code这个问题。

I have been looking online for advice, but there hasn't been a conclusive one that helped me. Please help. I didn't add any code behind related to this issue.

感谢您

推荐答案

您的问题是与DDL属性的SelectedValue ='&LT;%#绑定(称号)%&GT;

Your issue is with the DDL attribute SelectedValue='<%# Bind("title") %>'

您的SQL数据源 SqlDataSource1 可能不会返回任何来自先生,太太,小姐或MS值的。

Your SQL DataSource "SqlDataSource1" may not be returning any of the values from Mr, Mrs, Miss or MS.

如果标题字段中有你得到这个错误不同的值

If Title field has a different value you get this error

更新

您可以绑定之前如下验证您的标题字符串。但是,我不建议这一点。我preFER如果你可以从你的数据源检索之前验证值。理想的解决办法是找到它的值之前,用同一套价值观的DropDownList的绑定。那么你将不会错过任何东西。

You may validate your Title string as below before binding. But, I wouldn't recommend this. I prefer if you could validate the values before retrieving from your data source. The ideal solution would be to bind the DropDownList with same set of values before finding the values in it. then you won't miss anything.

下面是速战速决。 : - )

Here's the quick fix. :-)

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

更新2
在这里你去:-D

UPDATE 2 Here you go :-D

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

请接受答案如果它解决您的问题。所有最优秀的!

Please accept the answer if it resolves your issue. All the best!

这篇关于'detailsUserTitle'具有的SelectedValue,因为它不在项目列表中存在这是无效的。参数名:价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆