gridview delete row event firing twice?
-
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?
-
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?
Ok... did you put your
OnRowDeleting="GridView1_RowDeleting"
attribute in your ASPX and also register the event withGridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting);
?? That would fire it twice... I guess.
var question = (_2b || !(_2b));
-
Ok... did you put your
OnRowDeleting="GridView1_RowDeleting"
attribute in your ASPX and also register the event withGridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting);
?? That would fire it twice... I guess.
var question = (_2b || !(_2b));
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.
-
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.
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));
-
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));
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.