我关注了gridview:
<asp:Panel ID="pnlScroll" runat="server" ScrollBars="Auto">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GVCart2" runat="server" AutoGenerateColumns="False" GridLines="Vertical" onrowcommand="CartUpdate">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="Product_Name" HeaderText="Product Name" />
<asp:BoundField DataField="Product_ID" HeaderText="Product ID" />
<asp:BoundField DataField="ItemQTY" HeaderText="ItemQTY" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="IncreaseCartQty" runat="server" CommandArgument="<%#((GridViewRow)Container).RowIndex %>" CommandName="IncreaseCartQty" Text="+" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Price" HeaderText="Price" />
<asp:BoundField DataField="TotalPrice" HeaderText="TotalPrice" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
gridview 有一个按钮来增加项目数量。该按钮的命令名称为"IncreaseCartQty"。OnRowCommand 我正在尝试调用"CartUpdate"。但问题是OnRowCommand事件没有触发。
代码后面的页面如下:
在页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ItemsCount"] != null)
{
CartDT = (DataTable)Session["cart"];
GVCart2.DataSource = CartDT;
GVCart2.DataBind();
}
}
我想在 gridview OnRowCommand 上调用的函数是:
public void CartUpdate(object sender, GridViewCommandEventArgs e)
{
CartDT = (DataTable)Session["cart"];
if (e.CommandName == "IncreaseCartQty")
{
DataRow DR = CartDT.NewRow();
int rowIndex = Convert.ToInt32(e.CommandArgument.ToString());
int qty = Convert.ToInt32(CartDT.Rows[rowIndex][2].ToString());
DR[0] = CartDT.Rows[rowIndex][0];
DR[1] = CartDT.Rows[rowIndex][1];
DR[2] = ++qty;
DR[3] = CartDT.Rows[rowIndex][3];
DR[4] = qty * double.Parse(CartDT.Rows[rowIndex][3].ToString());
CartDT.Rows.RemoveAt(rowIndex);
CartDT.Rows.InsertAt(DR, rowIndex);
CartDT.AcceptChanges();
Session["cart"] = CartDT;
GVCart2.DataSource = CartDT;
GVCart2.DataBind();
TotalOrderAmt();
TotalItemsCount();
}
}
计算订单金额和商品数量的函数如下:
public void TotalOrderAmt()
{
double t = 0;
for (int i = 0; i < CartDT.Rows.Count; i++)
{
t = t + double.Parse(CartDT.Rows[i][4].ToString());
}
Session["TotalOrderAmt"] = t;
}
public void TotalItemsCount()
{
double t = 0;
for (int i = 0; i < CartDT.Rows.Count; i++)
{
t = t + double.Parse(CartDT.Rows[i][4].ToString());
}
Session["ItemsCount"] = t;
}
现在我无法理解为什么OnRowCommand事件没有触发。单击gridview中的按钮没有任何反应。
请让我知道哪里错了。