Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i have no idea on how to resolve gridview events are executing twice with one hit like, rowupdating,rowcanceling and rowdeleting events please give me solution i am facing lot of problems.
i tried like,
cases:

case1)In

page_load()
{
  (!page.ispostback)
   // code to execute at first time

}


case2). In gridview events like row_updating etc.
 {
  try
  {
   // code for row updating
  }
  catch
  {
  }
  Finally
  {
     e.cancel=true
  }
}


case3):
executed on both browesrs are like IE and Google crome eventough i am getting same problem, with these possibilities also i am not getting solutions so please concentrate on this problem and i am sending code in below please view my code.

In webpage contains the following code:
Page_Load(object sender, System.EventArgs e)
{
	if (Session["OrgCode"] == null) {
		Response.Redirect("index.aspx");
	}
	if (!Page.IsPostBack) {
		loadCourse();
		loadSection();
		loadClass();
	}
}

gvClass_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
	try {
		int admno = Convert.ToInt32(gvClass.DataKeys(e.RowIndex).Values("AdmNo").ToString());
		//Dim stuName As String = gvClass.DataKeys(e.RowIndex).Values("AdmNo").ToString()
		cn.ConnectionString = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString;
		cn.Open();
		SqlCommand cmd = new SqlCommand("delete from AdmTable where AdmNo=" + admno, cn);
		int result = cmd.ExecuteNonQuery();
		cn.Close();
		if (result == 1) {
			BindDataGrid1();
			lblresult.ForeColor = Color.Green;
			lblresult.Text = "Admission Number" + admno + " details deleted successfully";
		}
	} catch (Exception ex) {
		ClientScript.RegisterStartupScript(this.GetType, "alert", "javascript:alert('" + ex.Message + "');", true);
	}
}
protected void gvClass_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
	try {
		gvClass.EditIndex = e.NewEditIndex;
		BindDataGrid1();
	} catch (Exception ex) {
		ClientScript.RegisterStartupScript(this.GetType, "alert", "javascript:alert('" + ex.Message + "');", true);

	}

}


please give me solution.
Posted
Updated 13-Mar-12 22:26pm
v2

SQL
Remove
AutoEventWireup="true"

in the Page directives from both the aspx form AND the Master page
or
 method that needs to be called at the begninning of the event handling method:

 

    private bool Ok2Delete(int ri) // ri is the record index to be deleted
    {
        if (Session["ri"] == null ||
            (!((ri == ((int)Session["ri"])) &&
            (DateTime.Now.Subtract((DateTime)Session["ri_time_stamp"]).Seconds < 2))))
        {
            Session["ri"] = ri;
            Session["ri_time_stamp"] = DateTime.Now;
            return true;
        }
        return false;
    }
Example on how to use it:

 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (!Ok2Delete(e.RowIndex)) return;    

        // your logic goes here and the above IF statement 
        // will hopefully guarantee your code to run once.
    }
 
Share this answer
 
Comments
Madhugundi 14-Mar-12 6:07am    
Thanks
Hard to say what's wrong, but I can just give one very general advice, which usually works. Such situation with double call is very usual, but the reason of it is usually pretty easy to detect.

Use the debugger. Locate the call which is supposed to happens just once per click but actually happens twice. Put a breakpoint on the method called (not on the call), on the very first statement of the body of this method. Run the application. When the executions stops at the breakpoint, open the debug window "Call stack", then continue to run it. When the execution stops at the same breakpoint again, inspect the call stack — it will show where the extra call comes from. Learn in and then analyze the situation. It helps in nearly 100% of cases.

—SA
 
Share this answer
 
v2
Comments
Madhugundi 14-Mar-12 6:08am    
thanks
Sergey Alexandrovich Kryukov 14-Mar-12 12:38pm    
Would you consider accepting this answer formally (green button)? -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900