开发者

dropdown list in edit and read-only mode

Hello i have a dropdown list in edit mode and read only mode. If users upload pictures, i want them to chose a category for the uploaded pics and display it in a gridview as below.(See Picture below). When i include "SelectedValue='<%# Bind("CategoryID")" in edit and item template mode, i get this error "'PictureReadOnlyCategories' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "

When i removed the "SelectedValue='<%# Bind("CategoryID")" from edit and item template, i get the result on the pic below(screenshot). If u can see the pic below the category is not selected, it just display the -- No Category -- even when i chose a category for the pic.

I want when a pic is uploaded and i category chosen, to display on the gridview. The code for the error message is below:

            <EditItemTemplate>
                <asp:DropDownList ID="pictureEditCategories" runat="server" 
                    AppendDataBoundItems="True"
              DataSourceID="categoriesDataSource" 
                    DataTextField="Name" DataValueField="CategoryID" 
                    SelectedValue='<%# Bind("CategoryID") %>' ValidationGroup="PictureEdit" >
                    <asp:ListItem Value="" Text="--No Category -- "/>
                </asp:DropDownList>
            </EditItemTemplate>

            <ItemTemplate>
                <asp:DropDownList ID="PictureReadOnlyCategories" runat="server" 
                    AppendDataBoundItems="True" DataSourceID="categoriesDataSource" 
                    DataTextField="Name" DataValueField="CategoryID" Enabled="False" 
                    SelectedValue='<%# Bind("CategoryID") %>' ValidationGroup="PictureEdit" 
                     >
                    <asp:ListItem 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" 开发者_Go百科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>

When i remove the SelectedValue='<%# Bind("CategoryID") %>' from the item and edit template, I get the result for the picture below. Please i will appreciate your help.

Please you can edit the code or explain it to me since im still learning ASP.net

dropdown list in edit and read-only mode


question answered

You have a DataSource defined with a parameterized query that contains two parameters:

@CategoryID @UserID

However, you appear to only be populating one of the parameters via the Control Parameters.

Fixing this should be easy enough as you are already pulling the UserID from a query string variable in your other DataSource. You should be able to just copy the QueryStringParameter into your list of SelectParameters for your photo DataSource. Highlighted below:

You could also handle the Selecting event of the datasource and programatically set the UserID in the query that way, but the aforementioned option is easiest. I just want you to be aware of all your options.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜