2013-03-22 21 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個解決方案和解決方法,也許只是看看數據庫有時候會有訣竅。

相關問題