C# “detailsUserTitle”的SelectedValue无效,因为它不在项目列表中。参数名称:value
DropDownList不断返回此错误: 这是我的aspx代码C# “detailsUserTitle”的SelectedValue无效,因为它不在项目列表中。参数名称:value,c#,asp.net,drop-down-menu,C#,Asp.net,Drop Down Menu,DropDownList不断返回此错误: 这是我的aspx代码 <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="username" DataSourceID="SqlDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="Non
<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>
我一直在网上寻求建议,但没有一个结论能帮到我。请帮忙。我没有添加任何与此问题相关的代码
谢谢您的问题在于DDL属性SelectedValue= 您的SQL数据源SqlDataSource1可能没有从Mr、Mrs、Miss或MS返回任何值 如果标题字段具有不同的值,则会出现此错误 更新 您可以在绑定之前验证标题字符串,如下所示。但是,我不建议这样做。我更喜欢在从数据源检索之前验证这些值。理想的解决方案是在找到DropDownList中的值之前,将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值。也许我应该做一个装订检查?但我不知道怎么做在这种情况下,我如何才能检查标题是否有不同的值?嘿,山姆,谢谢你的快速修复。但是我从那个快速修复程序中得到了一个编译错误,它说:描述:在编译服务于这个请求所需的资源时发生了一个错误。请查看以下特定错误详细信息,并适当修改源代码。编译器错误消息:CS0103:名称“Bind”在当前上下文HMMM中不存在,是否可以尝试使用Evaltitle而不是Bindtitle?嘿,Sam,我向SelectedValue添加Eval的唯一方法是将其放入最后一个绑定中,如so SelectedValue='-1?Evaltitle:-1%>“我收到了相同的编译错误~谢谢:你是什么意思?您应该能够用Eval替换所有Bind语句