富有挑战性的Gridview事件一击即执行两次 [英] Challenging Gridview events are executing twice with one hit

查看:49
本文介绍了富有挑战性的Gridview事件一击即执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何解决一次两次击中网格视图事件(例如rowupduping,rowcanceling和rowdeleting事件)的麻烦,请给我解决方案,我遇到了很多问题.
我尝试过,
案例:

case1)在

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).在gridview事件中,例如row_updating等.



case2). In gridview events like row_updating etc.

 {
  try
  {
   // code for row updating
  }
  catch
  {
  }
  Finally
  {
     e.cancel=true
  }
}



case3):
在两个浏览器上执行的操作类似IE和Google crome,尽管我遇到了同样的问题,但由于这些可能性,我也没有得到解决方案,因此请专注于此问题,并在下面发送代码,请查看我的代码.

在网页中包含以下代码:



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.

推荐答案

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


很难说出什么问题,但是我只能给出一个非常普遍的建议,这种建议通常是可行的.这种带有两次通话的情况很常见,但其原因通常很容易发现.

使用调试器.找到应该每次单击仅发生一次但实际上发生两次的呼叫.在此方法的主体的第一个语句上,在被调用的方法(而不是调用)上放置一个断点.运行应用程序.当执行在断点处停止时,打开调试窗口调用堆栈",然后继续运行它.当执行再次在相同的断点处停止时,检查调用堆栈-它会显示额外调用的来源.学习,然后分析情况.它可以在近100%的情况下提供帮助.

—SA
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


这篇关于富有挑战性的Gridview事件一击即执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆