繁体   English   中英

FormView插入一个新的C#和ASP.NET行

[英]FormView inserting a new row C# and ASP.NET

我正在尝试使用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>

要进入插入模式,您必须在您的其他模板控制具有命令名称“新建”, 提供一些其它输入,可能会导致你的代码隐藏编程去(yourFormView).ChangeMode(FormViewMode.Insert) 然后在插入项模板中,您需要一个命令为“Insert”的按钮,或者一个可以编程方式转到(yourDataSource).Insert() 最后,您的数据源需要一个InsertCommand,其值是要执行的SQL,以及提供给它的一些参数规范,可以使用<InsertParameters>子标签完成,也可以在DataSource_Inserting事件处理程序中以编程方式完成。

第一步是进入formview的插入模式,第二步是将表单内容发送回数据源,第三步获取数据源将其写入数据库。

您需要在ItemTemplate一个按钮才能进入插入模式以及SqlDataSourceInsertCommand

  <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM