C# 详细视图问题中的DropDownList-ddl有一个无效的值,因为它不存在
我偶然发现了这个问题的类似案例,也遵循了解决方法。但我的情况很奇怪,因为我有两个不同的DetailsView控件,它们具有不同的数据,一个有效,另一个无效 这就是问题的细节。我收到以下错误消息:C# 详细视图问题中的DropDownList-ddl有一个无效的值,因为它不存在,c#,asp.net,C#,Asp.net,我偶然发现了这个问题的类似案例,也遵循了解决方法。但我的情况很奇怪,因为我有两个不同的DetailsView控件,它们具有不同的数据,一个有效,另一个无效 这就是问题的细节。我收到以下错误消息: DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value 我知道,也许我很愚蠢,没有看到什么。但也许你会。我有两个De
DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
我知道,也许我很愚蠢,没有看到什么。但也许你会。我有两个DetailsView,它们基于一个用户的数据集绑定数据。两个DV在其EditItemTemplates中都有DropdownList控件,这些控件绑定此列的可能值。我使用SelectedValue=作为我的DropDownList模板,与在2个DVs中使用的方式完全相同
如前所述,我知道解决方案背后的代码,但我希望避免这些,以保持代码的干净性和一致性。我无法真正记录我为什么在一个DetailsView上使用变通方法,以及为什么在另一个DetailsView上不使用变通方法
以下是我的2个详细视图的代码:
<asp:DetailsView ID="dv_theme_ava" runat="server" Height="50px" Width="125px" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" DefaultMode="Edit" AutoGenerateEditButton="True" DataKeyNames="Pat_ID">
<Fields>
<asp:TemplateField HeaderText="Theme">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="theme" DataValueField="theme"
SelectedValue='<%# Bind("theme") %>'>
</asp:DropDownList>
<asp:Label ID="lolbel2" runat="server" Text='<%# Bind("theme") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [theme] FROM [gui_themes]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Avatar">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"
DataTextField="avatar" DataValueField="avatar">
</asp:DropDownList>
<asp:Label ID="lolbel" runat="server" Text='<%# Bind("avatar") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [avatar] FROM [gui_avatars]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [pat_gui_config] WHERE ([Pat_ID] = @Pat_ID)" DeleteCommand="DELETE FROM [pat_gui_config] WHERE [Pat_ID] = @Pat_ID"
InsertCommand="INSERT INTO [pat_gui_config] ([Pat_ID], [theme], [avatar]) VALUES (@Pat_ID, @theme, @avatar)"
UpdateCommand="UPDATE [pat_gui_config] SET [theme] = @theme, [avatar] = @avatar WHERE [Pat_ID] = @Pat_ID">
<DeleteParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="Pat_ID" SessionField="sel_pat_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
<asp:Parameter Name="Pat_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="dv_treat_edit" runat="server" AutoGenerateEditButton="True"
AutoGenerateRows="False" DataKeyNames="Tr_ID" DataSourceID="sql_newcat" DefaultMode="Edit"
Height="50px" Width="90%" AllowPaging="True" CssClass="dv_details" Style="margin: 0 auto;">
<Fields>
<asp:BoundField DataField="Tr_ID" HeaderText="Tr_ID" InsertVisible="False" ReadOnly="True"
SortExpression="Tr_ID" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:BoundField DataField="syn_ger" HeaderText="Display Name (German)" SortExpression="syn_ger" />
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="ddl_type0" runat="server" DataSourceID="sql_ddl_type0" DataTextField="type"
DataValueField="type" SelectedValue='<%# Bind("type") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_type0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [type] FROM [entry_type]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exclusive for Patient_ID">
<EditItemTemplate>
<asp:DropDownList ID="ddl_excl_pat0" runat="server" DataSourceID="sql_ddl_exclpat0"
DataTextField="Pat_ID" DataValueField="Pat_ID" SelectedValue='<%# Bind("custom_cat_for_Pat") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_exclpat0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [Pat_ID] FROM [patients]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<CommandRowStyle CssClass="dv_footer" />
<RowStyle CssClass="dv_tr" />
<PagerSettings Mode="NumericFirstLast" Position="Top" Visible="False" />
</asp:DetailsView>
<asp:SqlDataSource ID="sql_newcat" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [treat_cat]" DeleteCommand="DELETE FROM [treat_cat] WHERE [Tr_ID] = @Tr_ID"
InsertCommand="INSERT INTO [treat_cat] ([description], [syn_ger], [type], [custom_cat_for_Pat]) VALUES (@description, @syn_ger, @type, @custom_cat_for_Pat)"
UpdateCommand="UPDATE [treat_cat] SET [description] = @description, [syn_ger] = @syn_ger, [type] = @type, [custom_cat_for_Pat] = @custom_cat_for_Pat WHERE [Tr_ID] = @Tr_ID">
<DeleteParameters>
<asp:Parameter Name="Tr_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
<asp:Parameter Name="Tr_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
注意:数据模型非常简单。为了进行比较,我使用了两个字段主题和类型。这两个表在我的数据库中只有一列,包含字符串条目
现在,DDL类型可以很好地获取其项,并将SelectedValue绑定到数据源带来的DetailsView中的值。当我用SelectedValue绑定主题DDL时,我得到了错误。有趣的是:在同一个EditItemTemplate中,我设置了一个标签ID lolbel2:p来检查数据绑定。当然,当我从DDL中删除SelectedValue时,它会起作用。因此,没有DDL中的SelectedValue,我的输出如下
[DROPDOWNLIST]带有物品空间和魔法
[LABEL]使用文本魔术,因为这是我的测试用户的值
我错过什么了吗?我完全疯了吗?
所以,很抱歉第十次再次问这个问题,但我想了解我的代码是做什么的
提前谢谢!
康拉德。发现了问题,这就是当你像山羊一样倔强时所得到的 在调试带有HiddenField的变通方法时,我注意到与Label控件绑定方式相同的值后面有一些空格。特别是:我得到的不是狗,而是狗。虽然asp:标签中没有显示该值,但我想这就是为什么在DropDownList中找不到该值的原因 空白是从哪里来的?在我的SQL表中,我将主题和化身的列创建为nchar,而不是nvarchar。显然,当使用nchar作为数据类型时,元组中未使用的字符会被空格填充,或者说字段的宽度固定为x个字符 将数据类型更改为nvarchar帮助我消除了空白,现在DDL的数据绑定工作正常 我正在记录这一点,因为可能其他人也会在这一点上绊倒——因为有50种解决方案和解决办法,也许只是看一下数据库就可以了