'体育'具有一个无效的SelectedValue,因为它不存在于项列表中。参数名称:value

时间:2014-05-11 15:44:56

标签: c# asp.net

我是一般的编码新手,并且我需要使用Visual.net使用ASP.net创建一个简报网站。 C#(对不起,如果我的一些行话有误,我是新手)

我遇到上述错误,无法解决问题。

以下是编辑新闻页面的代码。从select命令可以看出,我有2个db内连接。我希望有一个下拉列表,以便作者可以选择目前在数据库中的运动,如果他们不知道这些数字代表什么,那么这些运动就没有用。我能够得到一个下拉列表,但是当我添加" SelectedValue"因此,当作者选择一项运动时,它会更新数据库,但这是我收到错误的时候。我在插入页面中有相同的代码串,它可以工作,所以任何帮助将不胜感激:)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="editnews.aspx.cs" Inherits="admin_editnews" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
            <link href="../css/bootstrap.css" rel='stylesheet' type='text/css' />
        <link href="../css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:SqlDataSource ID="newseditrows" runat="server" 
            ConnectionString="<%$ ConnectionStrings:newsconnection %>" 
            SelectCommand="SELECT News.headline, News.Image, News.paragraph, Sports.sport, News.Sport, News.date, News.lead, News.authorID, Sports.id
                            FROM News 
                            INNER JOIN Sports ON News.Sport = Sports.id
                            ORDER BY date DESC"
            UpdateCommand="UPDATE [News] SET [headline]=@headline, [Image]=@Image, [paragraph]=@paragraph, [Sport]=@sport, [date]=@date, [lead]=@lead, [authorid]=@authorid WHERE [id]=@id">

            <UpdateParameters>
                <asp:Parameter Name="headline" Type="String" />
                <asp:Parameter Name="Image" Type="String" />
                <asp:Parameter Name="paragraph" Type="String" />
                <asp:Parameter Name="Sport" Type="Int32" />
                <asp:Parameter Name="date" Type="String" />
                <asp:Parameter Name="lead" Type="String" />
                <asp:Parameter Name="authorid" Type="Int32" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>

        <asp:SqlDataSource ID="sportlist" runat="server" 
            ConnectionString='<%$ ConnectionStrings:newsconnection %>' 
            SelectCommand="SELECT [id], [sport] FROM [Sports]">

        </asp:SqlDataSource>

        <asp:ListView ID="displayedit" runat="server"
            DataSourceID="newseditrows"
            DataKeyNames="id">

            <AlternatingItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Label Text='<%# Eval("Image") %>' runat="server" ID="ImageLabel" /><br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("sport") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                <br />
                </span>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <span style=""><%--id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel1" /><br />--%>
                    Headline:
                    <asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
                    Image:
                    <asp:TextBox Text='<%# Bind("Image") %>' runat="server" ID="ImageTextBox" /><br />
                    Paragraph:<br />
                    <asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" 
                                 TextMode="MultiLine" Columns="50" Rows="8"/><br />
                    <br />Sport:
                    <%--<asp:TextBox ID="SportTextBox" runat="server" Text='<%# Bind("Sport") %>' />--%>
                    <asp:DropDownList ID="Sport" runat="server" 
                                        DataSourceID="sportlist" 
                                        DataTextField="sport" 
                                        DataValueField="id"
                                        SelectedValue='<%# Bind("Sport") %>' >

                    </asp:DropDownList>
                    <br />
                    <%--date:
                    <asp:TextBox Text='<%# Bind("date") %>' runat="server" ID="dateTextBox" /><br />--%>
                    Lead:
                    <asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
                    <%--authorID:
                    <asp:TextBox Text='<%# Bind("authorID") %>' runat="server" ID="authorIDTextBox" /><br />--%>
                    <asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
                    <br />
                </span>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <span>No data was returned.</span>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <span style="">headline:
                    <asp:TextBox Text='<%# Bind("headline") %>' runat="server" ID="headlineTextBox" /><br />
                    Image:
                    <asp:TextBox Text='<%# Bind("Image") %>' runat="server" ID="ImageTextBox" /><br />
                    paragraph:
                    <asp:TextBox Text='<%# Bind("paragraph") %>' runat="server" ID="paragraphTextBox" /><br />
                    Sport:
                    <asp:TextBox Text='<%# Bind("sport") %>' runat="server" ID="SportTextBox" /><br />
                    date:
                    <asp:TextBox Text='<%# Bind("date") %>' runat="server" ID="dateTextBox" /><br />
                    lead:
                    <asp:TextBox Text='<%# Bind("lead") %>' runat="server" ID="leadTextBox" /><br />
                    authorID:
                    <asp:TextBox Text='<%# Bind("authorID") %>' runat="server" ID="authorIDTextBox" /><br />
                    <asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
                    <br />
                </span>
            </InsertItemTemplate>
            <ItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Label Text='<%# Eval("Image") %>' runat="server" ID="ImageLabel" /><br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("sport") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                <br />
                </span>
            </ItemTemplate>
            <LayoutTemplate>
                <div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
                <div style="">
                    <asp:DataPager runat="server" ID="DataPager1">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
                            <asp:NumericPagerField></asp:NumericPagerField>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
                        </Fields>
                    </asp:DataPager>

                </div>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <span style="">id:
                    <asp:Label Text='<%# Eval("id") %>' runat="server" ID="idLabel" /><br />
                    headline:
                    <asp:Label Text='<%# Eval("headline") %>' runat="server" ID="headlineLabel" /><br />
                    Image:
                    <asp:Label Text='<%# Eval("Image") %>' runat="server" ID="ImageLabel" /><br />
                    paragraph:
                    <asp:Label Text='<%# Eval("paragraph") %>' runat="server" ID="paragraphLabel" /><br />
                    Sport:
                    <asp:Label Text='<%# Eval("sport") %>' runat="server" ID="SportLabel" /><br />
                    date:
                    <asp:Label Text='<%# Eval("date") %>' runat="server" ID="dateLabel" /><br />
                    lead:
                    <asp:Label Text='<%# Eval("lead") %>' runat="server" ID="leadLabel" /><br />
                    authorID:
                    <asp:Label Text='<%# Eval("authorID") %>' runat="server" ID="authorIDLabel" /><br />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    <br />
                <br />
                </span>
            </SelectedItemTemplate>
        </asp:ListView>

    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

<强> ListControl.SelectedValue: 获取列表控件中所选项的值,或选择列表控件中包含指定值的项。 (继承自ListControl。) MSDN Reference

  

在这里,您尝试选择值字段中不存在的值。

SelectedValue='<%# Bind("id") %>'应该有用。

<asp:DropDownList ID="Sport" runat="server" 
                                    DataSourceID="sportlist" 
                                    DataTextField="sport" 
                                    DataValueField="id"
                                    SelectedValue='<%# Bind("id") %>' >