Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. gridview delete row event firing twice?

gridview delete row event firing twice?

Scheduled Pinned Locked Moved ASP.NET
helpcsharpcssasp-netdatabase
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hi Everyone, I've setup a simple shopping application and have created a shopping cart using a session DataTable. I've added a delete button column to my grid and then supplied code to remove the indexed row when someone clicks the button. What's weird, is I keep receiving an error indicating that the selected row index does not exist! when I place a debug on my code, it appears as though the logic is working correctly, but right after the row is deleted, it fires the page_onload event, works through all that logic (which reloads the new updated datatable), and then tries to delete the row again. Here's my page_onLoad:

    protected void Page_Load(object sender, EventArgs e)
    {
    double est_ship = 100.00;
    //try to load the cart from session memory. hit empty cart functions if not found / empty
    try
    {
    DataTable dt1 = (DataTable)Session["myCart"];
    if (dt1.Rows.Count > 0)
    {
    itemsGrid.DataSource = dt1;
    itemsGrid.DataBind();
    double subt = 0.0;
    foreach (System.Data.DataRow row in dt1.Rows)
    {
    subt += Convert.ToDouble(row["LineTotal"]);
    }
    double tax = (subt * 0.06);
    sub_total_lbl.Text = subt.ToString("c");
    tax_lbl.Text = tax.ToString("c");
    est_ship_lbl.Text = est_ship.ToString("c");
    total_lbl.Text = (subt + tax + est_ship).ToString("c");
    make_order_btn.Enabled = true;
    }
    else
    {
    emptyCart();
    }
    }
    catch (NullReferenceException)
    {
    emptyCart();
    }

    the emptyCart method simply set's the various labels on the page to indicate there's no items in the cart. and here's the row_deleting event:

    protected void itemsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    DataTable dt1 = (DataTable)Session["myCart"];
    dt1.Rows.Remove(dt1.Rows[e.RowIndex]);
    }

    I've been looking at the asp.net page lifecycle diagrams, but from what I've seen there it doesn't explain why the row_deleting event is being executed twice. Could it be a control issue?

    G 1 Reply Last reply
    0
    • C compninja25

      Hi Everyone, I've setup a simple shopping application and have created a shopping cart using a session DataTable. I've added a delete button column to my grid and then supplied code to remove the indexed row when someone clicks the button. What's weird, is I keep receiving an error indicating that the selected row index does not exist! when I place a debug on my code, it appears as though the logic is working correctly, but right after the row is deleted, it fires the page_onload event, works through all that logic (which reloads the new updated datatable), and then tries to delete the row again. Here's my page_onLoad:

      protected void Page_Load(object sender, EventArgs e)
      {
      double est_ship = 100.00;
      //try to load the cart from session memory. hit empty cart functions if not found / empty
      try
      {
      DataTable dt1 = (DataTable)Session["myCart"];
      if (dt1.Rows.Count > 0)
      {
      itemsGrid.DataSource = dt1;
      itemsGrid.DataBind();
      double subt = 0.0;
      foreach (System.Data.DataRow row in dt1.Rows)
      {
      subt += Convert.ToDouble(row["LineTotal"]);
      }
      double tax = (subt * 0.06);
      sub_total_lbl.Text = subt.ToString("c");
      tax_lbl.Text = tax.ToString("c");
      est_ship_lbl.Text = est_ship.ToString("c");
      total_lbl.Text = (subt + tax + est_ship).ToString("c");
      make_order_btn.Enabled = true;
      }
      else
      {
      emptyCart();
      }
      }
      catch (NullReferenceException)
      {
      emptyCart();
      }

      the emptyCart method simply set's the various labels on the page to indicate there's no items in the cart. and here's the row_deleting event:

      protected void itemsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
      DataTable dt1 = (DataTable)Session["myCart"];
      dt1.Rows.Remove(dt1.Rows[e.RowIndex]);
      }

      I've been looking at the asp.net page lifecycle diagrams, but from what I've seen there it doesn't explain why the row_deleting event is being executed twice. Could it be a control issue?

      G Offline
      G Offline
      Greg Chelstowski
      wrote on last edited by
      #2

      Ok... did you put your OnRowDeleting="GridView1_RowDeleting" attribute in your ASPX and also register the event with

      GridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting);

      ?? That would fire it twice... I guess.

      var question = (_2b || !(_2b));

      C 1 Reply Last reply
      0
      • G Greg Chelstowski

        Ok... did you put your OnRowDeleting="GridView1_RowDeleting" attribute in your ASPX and also register the event with

        GridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting);

        ?? That would fire it twice... I guess.

        var question = (_2b || !(_2b));

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        No, I only have it listed in the aspx attribute OnRowDeleting="GridView1_RowDeleting" I wonder if ripping it out of the ASPX page and registering in the code behind would make a difference? hmm...

        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

        G 1 Reply Last reply
        0
        • C compninja25

          No, I only have it listed in the aspx attribute OnRowDeleting="GridView1_RowDeleting" I wonder if ripping it out of the ASPX page and registering in the code behind would make a difference? hmm...

          Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

          G Offline
          G Offline
          Greg Chelstowski
          wrote on last edited by
          #4

          DataTable dt1 = (DataTable)Session["myCart"]; if(dt1.Rows[e.RowIndex]!=null) { dt1.Rows.Remove(dt1.Rows[e.RowIndex]); } should take care of it i guess... I know it's not the best of solutions, but at least it should work.

          var question = (_2b || !(_2b));

          C 1 Reply Last reply
          0
          • G Greg Chelstowski

            DataTable dt1 = (DataTable)Session["myCart"]; if(dt1.Rows[e.RowIndex]!=null) { dt1.Rows.Remove(dt1.Rows[e.RowIndex]); } should take care of it i guess... I know it's not the best of solutions, but at least it should work.

            var question = (_2b || !(_2b));

            C Offline
            C Offline
            compninja25
            wrote on last edited by
            #5

            Hi Greg, Thank's for the work around. I was googling it again and found this article: forums.asp.net Looks like there is a known bug when using an image instead of a regular text hyperlink! I would have never guessed that!

            Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups