DetailsView-Issue - ddl中的下拉列表有一个无效的值,因为它不存在

本文关键字:因为 不存在 无效 有一个 ddl 下拉列表 DetailsView-Issue | 更新日期: 2023-09-27 18:05:15

我偶然发现了类似的问题,也遵循了解决方法。但我的情况是相当奇怪的,因为我有2个不同的DetailsView-controls(与不同的数据),一个工作,另一个没有。

这就是问题的细节。我得到以下错误消息:

DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

我知道这个线程,也许我是愚蠢的,没有看到一些东西。但也许你知道。我有2个Detailsviews,它们基于一个用户的数据集绑定它们的数据。这两个DVs在它们的edittitemtemplates中都有下拉列表控件,它们绑定了这个列的可能值。我使用SelectedValue='<%# Bind("mycolumn") %>'作为我的dropdownlist模板,在2个DVs中完全相同的方式。

如前所述,我知道代码隐藏的解决方案,但我想避免这些,以保持我的代码干净和一致。我真的不能说明为什么我在一个DetailsView上使用了变通方法,而在另一个上不使用。

下面是我的2个DetailsViews的代码:

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

注意:数据模型非常简单。为了比较,我使用了"主题"answers"类型"两个字段。这两个表在我的数据库中只有1列,用于保存字符串条目。

现在,"type"-DDL可以很好地获取它的项,并将SelectedValue绑定到数据源带给DetailsView的值。当我将"主题"-DDL与SelectedValue绑定时,我得到了错误。有趣的是:在同一个EditItemTemplate中,我已经设置了一个标签(ID"lolbel2":p)来检查数据绑定。它可以工作(当然,当我从DDL中删除SelectedValue时)。如果没有DDL中的SelectedValue,我的输出是这样的[下拉列表]包含"space", "magic"[LABEL]与文本"magic"(因为这是我的test-user的值)。

我错过了什么吗?我是不是完全疯了?所以,很抱歉第十次重复问这个问题,但我想了解我的代码是做什么的。

提前感谢!康拉德

DetailsView-Issue - ddl中的下拉列表有一个无效的值,因为它不存在

好吧。找到了问题所在,这就是你像山羊一样固执的结果。:)

在调试HiddenField的解决方案时,我注意到该值与Label-Control的绑定方式相同,末尾有一些空白。特别是:我用"dog"代替了"dog"。虽然这在asp:Label中没有显示,但我想这就是为什么在下拉列表中没有找到该值的原因。

空格从何而来?在我的SQL表中,我为"主题"answers"化身"创建了"nchar"列,而不是"nvarchar"。显然,当使用"nchar"作为数据类型时,元组中未使用的字符被空白填充,或者让我们说字段具有固定的宽度(总是x个字符)。

将数据类型更改为"nvarchar"帮助我摆脱了空白,现在ddl的数据绑定工作得很好。

我正在记录这一点,因为也许其他人也会遇到这个问题-并且由于有50种解决方案和变通方法,也许只是看看数据库有时会奏效。

相关文章: