c#和ASP.NET的FormView插入新行

本文关键字:FormView 插入 新行 NET ASP | 更新日期: 2023-09-27 18:10:30

我正试图使用FormView连接到我的SQL DB,我正在遵循指南,但在指南上说,如果我在ItemTemplate中应该有一个"新"按钮超链接进入InsertItemTemplate,我面临的问题是按钮不存在。我如何进入插入模式,这样我就可以添加一个新的记录到我的SQL数据库?将项目实际添加到数据库中的语法是什么?谢谢你的帮助

<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" />
            &nbsp;<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" />
            &nbsp;<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>

c#和ASP.NET的FormView插入新行

要进入插入模式,您必须或者在您的其他模板中包含一个具有命令名称"New"的控件,提供一些其他输入,可以导致您的代码隐藏以编程方式转(yourFormView).ChangeMode(FormViewMode.Insert)。然后在插入项模板中,您需要一个命令为"插入"的按钮,或者可以通过编程进入(yourDataSource).Insert()的控件。最后,数据源需要一个InsertCommand(其值是要执行的SQL)和提供给它的一些参数规范,这可以通过<InsertParameters>子标记完成,也可以在datasource_inserts事件处理程序中以编程方式完成。

第一步使您进入表单视图的插入模式,第二步使表单内容发送回数据源,第三步使数据源将其写入数据库。

你需要一个按钮在你的ItemTemplate进入插入模式以及InsertCommand在你的SqlDataSource:

  <asp:SqlDataSource ID="SqlDataSource1" runat="server"
          ConnectionString="<%$ ConnectionStrings:BlaConnectionString %>"
            InsertCommand="INSERT INTO [Customer] ([Name]) VALUES (@Name)"
            SelectCommand="SELECT [id], [Name] FROM [Customer]"
            OnInserted="SqlDataSource1_Inserted">
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>