简体   繁体   中英

Gridview OnRowCommand event not firing

I've following 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>

The gridview has one button to increase the item amount. The button has command name "IncreaseCartQty" . OnRowCommand I am trying to call "CartUpdate" . But the problem is that the OnRowCommand event is not firing .

The page behind code is as follows:

On Page load :

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

The function which I want to call on gridview OnRowCommand is:

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();
            }

        }

The functions to calculate order amt and items count is as:

 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;
        }

Now I am not able to understand why the OnRowCommand event is not firing . Nothing is happening on clicking the button in gridview.

Please let me know where exactly wrong.

The problem that you have is on the page load method. becouse you need the Ispostback method.

Replace that (see code below)

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

for that (see code below)

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

If you don't use the !Ispostback Method, It throws an error and does not execute the Onrowcommand argument.

I hope that helps.

EDIT:

For more help Invalid Postback on Grid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM