如何使用Object Datasource参数绑定Gridview更新

时间:2016-04-21 19:33:03

标签: c# asp.net gridview

我有一个带有列

的购物车的gridview
id | productName | productPrice | productQuantity | totalPrice 

数量是gridview中唯一可编辑的列。我需要将id和数量传递给数据源,我该怎么做?我是初学者,现在已经工作了一个小时,我们将非常感谢你的帮助

.aspx文件

   <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getListFromCart" TypeName="GetFromCart" DeleteMethod="deleteListFromCart" UpdateMethod="updateListFromCart"  >
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="quantity" Type="Int32" />

        </UpdateParameters>
        </asp:ObjectDataSource>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" CellPadding="10" CellSpacing="2" >
        <Columns>

            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ReadOnly="True" />
            <asp:BoundField DataField="productName" HeaderText="productName" SortExpression="productName" ReadOnly="True" />
            <asp:BoundField DataField="productPrice" HeaderText="productPrice" SortExpression="productPrice" ReadOnly="True" />
            <asp:TemplateField HeaderText="quantity" SortExpression="quantity">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("quantity") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("quantity") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="totalPrice" HeaderText="totalPrice" SortExpression="totalPrice"  ReadOnly="True" />
        </Columns>
        <EditRowStyle HorizontalAlign="Center" />
    </asp:GridView>

.cs文件

public static void updateListFromCart(int id,int quantity)
{
    using (SqlConnection conn = new SqlConnection(connection))
    {
        SqlCommand cmd = new SqlCommand("updateCartDetails", conn);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
        cmd.Parameters.Add("@quantity", SqlDbType.Int).Value = quantity;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }

}

0 个答案:

没有答案