下拉列表不会对数据库中的值进行轮询

时间:2010-10-04 04:12:38

标签: asp.net

我遇到了一个问题,即在只读模式下使用项目字段模板使用数据库中的值填充DropDownList控件。我很感激详细解释,因为我是ASP.NET的新手。下面是代码和我得到的错误:

  

'PictureReadOnlyCategories'有一个SelectedValue,它是无效的,因为它在项目列表中不存在。   参数名称:value描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

     

异常详细信息:System.ArgumentOutOfRangeException:'PictureReadOnlyCategories'具有SelectedValue,该ListValue无效,因为它在项列表中不存在。   参数名称:值

这个代码是:

<asp:SqlDataSource ID="categoriesDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand ="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = @UserId)">
    <SelectParameters>
        <asp:ControlParameter ControlID="UserIdValue" 
                              Name="UserId"
                              PropertyName="Text" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:Label ID="UserIdValue" runat="server" Visible="False"></asp:Label>

<asp:GridView ID="gvPictures"
              runat="server"
              AllowPaging="True" 
              AllowSorting="True"
              AutoGenerateColumns="False"
              CellPadding="4"
              DataKeyNames="PictureID"
              DataSourceID="picturesDataSource"
              ForeColor="#333333"
              GridLines="None" Width="800px"> 
    <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
        <asp:CommandField ShowDeleteButton="True"
                          ShowEditButton="True"
                          ValidationGroup="PictureEdit" /> 
         <asp:TemplateField HeaderText="Category"
                            SortExpression="CategoryID"> 
             <EditItemTemplate> 
                 <asp:DropDownList ID="pictureEditCategories"
                                   runat="server"
                                   AppendDataBoundItems="True"
                                   DataSourceID="categoriesDataSource"
                                   DataTextField="Name"
                                   DataValueField="CategoryID"
                                   SelectedValue='<%# Bind("CategoryID") %>'> 
                     <asp:ListItem Value="" Text="--Select Category -- "/> 
                 </asp:DropDownList> 
             </EditItemTemplate> 
             <ItemTemplate>
                 <asp:DropDownList ID="PictureReadOnlyCategories"
                                   runat="server"
                                   AppendDataBoundItems="True"
                                   DataSourceID="categoriesDataSource" 
                                   DataTextField="Name"
                                   DataValueField="CategoryID"
                                   Enabled="False"
                                   SelectedValue='<%# Bind("CategoryID") %>'>
                     <asp:ListItem Selected="True" Value="">-- No Category --</asp:ListItem> 
                </asp:DropDownList>
            </ItemTemplate> 
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Title" SortExpression="Title"> 
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1"
                             runat="server"
                             EnableViewState="False"
                             Text='<%# Bind("Title") %>'>
                </asp:TextBox> 
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4"
                                            runat="server"
                                            ControlToValidate="TextBox1"
                                            Display="Dynamic"
                                            ErrorMessage="must enter a title"
                                            ValidationGroup="PictureEdit">
                </asp:RequiredFieldValidator>
            </EditItemTemplate> 
            <ItemTemplate> 
                <asp:Label ID="Label1"
                           runat="server"
                           Text='<%# Bind("Title") %>'>
                </asp:Label> 
            </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="Description" SortExpression="Description"> 
            <EditItemTemplate> 
                <asp:TextBox ID="TextBox2"
                             runat="server"
                             Columns="25"
                             Rows="4"
                             Text='<%# Bind("Description") %>'
                             TextMode="MultiLine">
                </asp:TextBox> 
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5"
                                            runat="server"
                                            ControlToValidate="TextBox2"
                                            Display="Dynamic"
                                            ErrorMessage="you must enter a description"
                                            ValidationGroup="PictureEdit">
                </asp:RequiredFieldValidator><
            </EditItemTemplate> 
            <ItemTemplate>
                <asp:Label ID="Label2"
                           runat="server"
                           Text='<%# Bind("Description") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date Added" SortExpression="UploadedOn"> 
            <EditItemTemplate>
                <asp:Label ID="Label4"
                           runat="server"
                           Text='<%# Bind("UploadedOn") %>'>
                </asp:Label>
            </EditItemTemplate> 
            <ItemTemplate> 
                <asp:Label ID="Label3"
                           runat="server"
                           Text='<%# Bind("UploadedOn") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField> 
        <asp:ImageField DataImageUrlField="PictureID"
                        DataImageUrlFormatString="~/UploadedImages/{0}.jpg"
                        HeaderText="Image"
                        ReadOnly="True"> 
            <ControlStyle Width="100px" /> 
        </asp:ImageField>
    </Columns> 
    <EditRowStyle BackColor="#2461BF" /> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
    <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
    <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
    <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
    <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
 </asp:GridView>

1 个答案:

答案 0 :(得分:1)

您需要确保绑定PictureReadOnlyCategories DDL以填充之前绑定所选项目的项目。问题看起来就像订单正在完成的事情一样。如果您尝试在DDL中选择一个不存在的项目,则会出现该错误。