Click here to Skip to main content
12,356,455 members (63,748 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET
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 13-Mar-12 22:20pm
Edited 13-Mar-12 22:26pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
    }
  Permalink  
Comments
Madhugundi 14-Mar-12 6:07am
   
Thanks
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Madhugundi 14-Mar-12 6:08am
   
thanks
SAKryukov 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100