1

好吧,我偶然发现了这个问题的类似案例,并且也遵循了解决方法。但我的情况很奇怪,因为我有 2 个不同的 DetailsView 控件(具有不同的数据),一个有效,另一个无效。

所以这里是详细的问题。我收到以下错误消息:

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

我知道这个线程,也许我很愚蠢,没有看到任何东西。但也许你会。我有 2 个详细视图,它们根据一个用户的数据集绑定他们的数据。两个 DV 在其 EditItemTemplates 中都有 DropdownList-Controls,用于绑定此列的可能值。我将 SelectedValue='<%# Bind("mycolumn") %>' 用于我的 DropDownList 模板,与 2 个 DV 中的方式完全相同。

如前所述,我知道代码隐藏的解决方法,但我想避免这些,以保持我的代码干净和一致。我无法真正记录为什么我在一个 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>

注意:数据模型非常简单。为了比较,我使用了 2 个字段“主题”和“类型”。这两个表在我的数据库中只有 1 列,包含字符串条目。

现在,“类型”-DDL 可以很好地获取它的项目,并将 SelectedValue 绑定到数据源带到 DetailsView 的值。当我将“主题”-DDL 与 SelectedValue 绑定时,出现错误。有趣的是:在同一个 EditItemTemplate 中,我设置了一个标签(ID“lolbel2”:p)来检查数据绑定。它有效(当然,当我从 DDL 中删除 SelectedValue 时)。因此,如果没有 DDL 中的 SelectedValue,我的输出就像 [DROPDOWNLIST] 带有项目“space”、“magic” [LABEL] 带有文本“magic”(因为这是我的测试用户的值)。

我错过了什么吗?我完全疯了吗?所以,很抱歉第 10 次重新提出这个问题,但我想了解我的代码的作用。

提前致谢!康拉德

4

1 回答 1

0

哦哦哦。发现了问题,这就是当你像山羊一样固执的时候得到的。:)

在使用 HiddenField 调试解决方法时,我注意到与 Label-Control 绑定方式相同的值有一些尾随空格。特别是:而不是“狗”,我得到了“狗”。虽然这没有显示在 asp:Label 中,但我想这就是在 DropDownList 中找不到该值的原因。

空白是从哪里来的?在我的 SQL 表中,我将“主题”和头像的列创建为“nchar”,而不是“nvarchar”。显然,当使用“nchar”作为 DataType 时,元组的未使用字符用空格填充,或者假设字段具有固定宽度(始终为 x 字符)。

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

我正在记录这一点,因为也许其他人也会偶然发现这一点——而且由于有 50 种解决方案和变通方法,也许只看一下数据库有时就可以解决问题。

于 2013-03-25T12:01:05.157 回答