ASP.. NET批量编辑GridView保存按钮

本文关键字:GridView 保存 按钮 编辑 NET ASP | 更新日期: 2023-09-27 18:19:18

我使用这个批量编辑GridView控件http://aspnetrealworldcontr.codeplex.com/

我使用Linq并有一个存储过程来更新数据库。

我不明白如何使用保存按钮来更新数据库。您应该使用一个按钮更新所有行。

在这个例子中,他们使用了SqlDataSource

例子
        <div>
        <asp:Button runat="server" ID="SaveButton" Text="Save Data" />
        <rwg:BulkEditGridView ID="EditableGrid" DataSourceID="PubsDataSource" AutoGenerateColumns="False"
            DataKeyNames="au_id" SaveButtonID="SaveButton" runat="server">
            <Columns>
                <asp:BoundField HeaderText="Last Name" DataField="au_lname" />
                <asp:BoundField HeaderText="First Name" DataField="au_fname" />
                <asp:BoundField HeaderText="Phone" DataField="phone" />
                <asp:BoundField HeaderText="Address" DataField="address" />
                <asp:BoundField HeaderText="City" DataField="city" />
                <asp:BoundField HeaderText="State" DataField="state" />
                <asp:BoundField HeaderText="Zip Code" DataField="zip" />
                <asp:CheckBoxField HeaderText="Contract" DataField="contract" />
            </Columns>
        </rwg:BulkEditGridView>
        <asp:SqlDataSource ID="PubsDataSource" runat="server" 
            SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
            UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zip] = @zip, [contract]=@contract WHERE [au_id] = @au_id"
            ConnectionString="<%$ ConnectionStrings:Pubs %>"  />
    </div>

我的代码-只有数量可以更新。

            <cc1:BulkEditGridView ID="CartGrid" runat="server" CssClass="table table-condensed" AutoGenerateColumns="False" GridLines="None" SaveButtonID="UpdateShoppingCart" EnableInsert="False" InsertRowCount="1">
            <Columns>
                <asp:BoundField DataField="ArtnrFull" HeaderText="Artnr" ReadOnly="True" ItemStyle-Width="10%">
                    <ItemStyle Width="10%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="ProductName" HeaderText="Namn" ReadOnly="True" />
                <asp:BoundField DataField="Quantity" HeaderText="Antal" ItemStyle-Width="10%">
                    <ItemStyle Width="10%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Pris" DataFormatString="{0:c}" HeaderText="Pris" ReadOnly="True" ItemStyle-Width="20%">
                    <ItemStyle Width="20%"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="Subtotal" DataFormatString="{0:c}" HeaderText="Subtotal" ReadOnly="True" ItemStyle-Width="20%">
                    <ItemStyle Width="20%"></ItemStyle>
                </asp:BoundField>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete" Text="">
                            <asp:Image ID="Image1" runat="server" ImageUrl="/img/delete.png" Width="20" />
                        </asp:LinkButton>
                    </ItemTemplate>
                    <ItemStyle Width="5%" />
                </asp:TemplateField>
                <asp:TemplateField Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="LblShoppingCartId" runat="server" Text='<%# Bind("ShoppingCartId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </cc1:BulkEditGridView>
        <asp:Button ID="UpdateShoppingCart" runat="server" Text="Spara" CssClass="btn btn-success pull-right" />
存储过程

CREATE PROCEDURE ShoppingCartUpdateQuantity
@ShoppingCartId int,
@Quantity int,
@ArtnrFull varchar(20)
AS
UPDATE ShoppingCartItems
            SET Quantity = @Quantity
            WHERE ArtnrFull = @ArtnrFull AND ShoppingCartItems.ShoppingCartId = @ShoppingCartId

猜测,但这不会运行,当我点击按钮,网格只是消失。

        protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtQuantity = (TextBox)CartGrid.Rows[e.RowIndex].FindControl("txtQuantity");
        Label lblArtnr = (Label)CartGrid.Rows[e.RowIndex].FindControl("lblArtnr");
        Label lblShoppingCartId = (Label)CartGrid.Rows[e.RowIndex].FindControl("lblShoppingCartId");
        LinqtoDBDataContext db = new LinqtoDBDataContext();
        db.ShoppingCartUpdateQuantity(Convert.ToInt32(lblShoppingCartId.Text),Convert.ToInt32(txtQuantity.Text), lblArtnr.Text);
        CartGrid.EditIndex = -1;
        FillGrid(Convert.ToInt32(lblShoppingCartId.Text));
    }

ASP.. NET批量编辑GridView保存按钮

  protected void CartGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
        if (CartGrid.DirtyRows.Count > 0)
        {
            Label lblShoppingCartId = (Label)CartGrid.Rows[e.RowIndex].FindControl("lblShoppingCartId");
            var cartid = lblShoppingCartId.Text;
            //find out which rows were updated
            foreach (GridViewRow row in CartGrid.DirtyRows)
            {
                string lblArtnr = row.Cells[0].Text;
                TextBox txtQuantity = row.Cells[2].Controls[0] as TextBox;
                LinqtoDBDataContext db = new LinqtoDBDataContext();
                db.ShoppingCartUpdateQuantity(Convert.ToInt32(lblShoppingCartId.Text), Convert.ToInt32(txtQuantity.Text), lblArtnr);
            }
            FillGrid(Convert.ToInt32(cartid));
            alertdiv.Attributes.Add("class", "alert alert-success");
            alertdiv.Controls.Add(new LiteralControl("Kundvagnen updaterad"));
            alertdiv.Visible = true;
        }
        else
        {
            alertdiv.Attributes.Add("class", "alert alert-danger");
            alertdiv.Controls.Add(new LiteralControl("FEL! Kundvagnen ej updaterad"));
            alertdiv.Visible = true;
        }
        }
        catch (Exception)
        {
            alertdiv.Attributes.Add("class", "alert alert-danger");
            alertdiv.Controls.Add(new LiteralControl("FEL! Kundvagnen ej updaterad"));
            alertdiv.Visible = true;
        }
    }