【问题标题】:ASP.NET Bulk Edit GridView Save buttonASP.NET 批量编辑 GridView 保存按钮
【发布时间】:2014-10-18 18:18:44
【问题描述】:

我使用这个 Bulk Edit 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));

    }

【问题讨论】:

    标签: c# asp.net linq gridview bulkinsert


    【解决方案1】:
      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;
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多