Gridview RowCommand事件未触发

时间:2013-05-27 13:48:48

标签: c# asp.net gridview

我关注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中的按钮时没有任何事情发生。

请让我知道哪里出错。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是页面加载方法。因为你需要Ispostback方法。

替换它(见下面的代码)

 protected void Page_Load(object sender, EventArgs e)
    {
      if (Session["ItemsCount"] != null)
      {
       CartDT = (DataTable)Session["cart"];
       GVCart2.DataSource = CartDT;
       GVCart2.DataBind();
      }
    }

(见下面的代码)

 protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        if (Session["ItemsCount"] != null)
        {
         CartDT = (DataTable)Session["cart"];
         GVCart2.DataSource = CartDT;
         GVCart2.DataBind();
        }
      }
    }

如果不使用!Ispostback方法,则会抛出错误并且不执行Onrowcommand参数。

我希望有所帮助。

修改

获取更多帮助Invalid Postback on Grid