ASP.net详细信息视图列未插入数据库

时间:2014-05-15 16:17:34

标签: c# asp.net webforms

我正在尝试使用Details View将项目插入数据库。我正在使用dropdownList来插入CategoryID列,因为它是一个外键,我不希望用户能够插入他们想要的任何数字。下拉列表从Categories表中的主键获取它的值。由于某种原因,正在插入其他行,但categoryID在数据库中具有空值。我不确定我做错了什么,任何帮助都会受到赞赏。这是设计页面中的一些代码。

        <asp:SqlDataSource ID="sqlDetailsViewDDl" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Id] FROM [Categories]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Categories] WHERE [Id] = @original_Id" OldValuesParameterFormatString="original_{0}">
        <DeleteParameters>
            <asp:Parameter Name="original_Id" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>




</div>
    <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" OnItemDeleting="DetailsView1_ItemDeleting" Width="250px">
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
            <asp:TemplateField HeaderText="ID">
                <EditItemTemplate>      
                    <asp:DropDownList  ID="DropDownList1" runat="server"  DataSourceID="sqlDetailsViewDDl" DataTextField="Id" DataValueField="Id" AutoPostBack="True"></asp:DropDownList>
                </EditItemTemplate>

                <ItemTemplate>                                            
                        <asp:Label ID="CategoryLabel" Text='<%# Eval("CategoryID") %>' runat="server" ></asp:Label>

                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>

1 个答案:

答案 0 :(得分:0)

想出来,在dropdownList

中遗漏了这个
 SelectedValue='<%# Bind("CategoryID") %>'