2013-03-22 54 views
1

以及我已经偶然发现了这个问题的类似情况,并且也遵循了解决方法。但是我的情况很奇怪,因为我有两个不同的DetailsView控件(具有不同的数据),一个正常工作,另一个不工作。DetailsView中的DropDownList-Issue-ddl有一个无效的值,因为它不存在

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

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

我知道this thread,也许我傻,而不是看到的东西。但也许你会。我有2个Detailsviews,它们根据一个用户的数据集绑定他们的数据。两个DV在它们的EditItemTemplates中都有DropdownList-Controls,它们将这个列的可能值绑定在一起。我使用SelectedValue ='<%#绑定(“mycolumn”)%>'为我的DropDownList模板在两个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绑定到由DataSource引入DetailsView的值。当我使用SelectedValue绑定“主题”-DDL时,出现错误。有趣的是:在同一个EditItemTemplate中,我设置了一个Label(ID“lolbel2”:p)来检查数据绑定。它的工作原理(当然,当我从DDL中删除SelectedValue)。因此,如果没有DDL中的SelectedValue,我的输出就像 [DROPDOWNLIST]带有项目“space”,“magic” [LABEL]带有文本“magic”(因为这是我的测试用户的值)。

我错过了什么吗?我完全坚强吗? 因此,对于像第10次那样重新提出这个问题感到抱歉,但我想了解我的代码的功能。

在此先感谢! Konrad

回答

0

Ooooohkay。发现问题,那就是当你像山羊一样固执时得到的。 :)

当使用HiddenField调试解决方法时,我发现与Label控件绑定的值具有一些尾随空格。特别是:而不是“狗”,我得到了“狗”。虽然这没有显示在asp:Label中,但我猜这就是在DropDownList中找不到该值的原因。

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

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

我记录了这一点,也许别人会偶然发现这一点 - 因为有50个解决方案和解决方法,也许只是看看数据库有时候会有诀窍。

相关问题