C# 未将ASP.net详细信息视图列插入数据库
我正在尝试使用“详细信息”视图将项目插入数据库。我使用dropdownList插入CategoryID列,因为它是一个外键,我不希望用户能够插入他们想要的任何数字。dropdownlist从Categories表中的主键获取其值。出于某种原因,正在插入其他行,但categoryID在数据库中具有空值。我不确定我做错了什么,任何帮助都将不胜感激。下面是设计页面中的一些代码C# 未将ASP.net详细信息视图列插入数据库,c#,asp.net,webforms,C#,Asp.net,Webforms,我正在尝试使用“详细信息”视图将项目插入数据库。我使用dropdownList插入CategoryID列,因为它是一个外键,我不希望用户能够插入他们想要的任何数字。dropdownlist从Categories表中的主键获取其值。出于某种原因,正在插入其他行,但categoryID在数据库中具有空值。我不确定我做错了什么,任何帮助都将不胜感激。下面是设计页面中的一些代码 <asp:SqlDataSource ID="sqlDetailsViewDDl" runat="ser
<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>
找到了答案,但在dropdownList中没有找到
SelectedValue='<%# Bind("CategoryID") %>'
SelectedValue=''
如果您不知道,直接在视图中定义数据库访问绝对是一个可怕的想法。如果您想正确地执行此操作,您应该为此创建一个存储库,这样应用程序的前端就不知道数据来自何处,也不知道连接字符串之类的细节,它只是公开一些方法,例如GetCategories
。