我想用一個FormView
連接到我的SQL數據庫,我按照指南,但在指南上說,如果我在ItemTemplate
應該有一個「新「按鈕超鏈接進入InsertItemTemplate
,我面臨的問題是該按鈕不存在。我將如何進入插入模式,以便可以將新記錄添加到我的SQL數據庫中?實際將項目添加到數據庫中的語法是什麼?感謝您的幫助FormView插入一個新行C#和ASP.NET
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" CellPadding="4" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." ForeColor="#333333"
Height="250px" Width="957px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField ApplyFormatInEditMode="True" DataField="Date_Added"
DataFormatString="{0:MMM d, yyyy}" HeaderText="Date_Added" HtmlEncode="False"
SortExpression="Date_Added" />
</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>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>"
SelectCommand="SELECT [ID], [Title], [Description], [Date Added] AS Date_Added FROM [knowledgebase]">
</asp:SqlDataSource>
<br />
</div>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1">
<EditItemTemplate>
ID:
<asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
<br />
Title:
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:TextBox ID="Date_AddedTextBox" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
Title:
<asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:TextBox ID="DescriptionTextBox" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:TextBox ID="Date_AddedTextBox" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
ID:
<asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
<br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Bind("Description") %>' />
<br />
Date_Added:
<asp:Label ID="Date_AddedLabel" runat="server"
Text='<%# Bind("Date_Added") %>' />
<br />
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
您需要在ItemTemplate中帶有commandname =「New」的按鈕才能切換到[FormViewMode Insert](http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.formviewmode.aspx )。 –
好吧我想出瞭如何進入InsertMode,但現在我收到一個錯誤,當我添加一條記錄。說:「除非指定了InsertCommand,否則數據源'SqlDataSource1'不支持插入。」 這是我正在使用的插入模板: ' Insert asp:LinkButton>' –
KPS
您可以將代碼顯示到您的sqldatasource1 – atbebtg