这是我测试页的代码:
public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource2.SelectCommand = "select * From t_94";
            SqlDataSource2.DeleteCommand = "delete from t_94 where id = @id";
            GridView3.DataSource = SqlDataSource2;            GridView3.DataBind();
            Response.Write("<script language=javascript> alert('load') </script>");
        }        protected void GridView3_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlDataSource2.DeleteParameters.Add("id", GridView3.DataKeys[e.RowIndex].Value.ToString());
            SqlDataSource2.Delete();
            GridView3.DataBind();
            Response.Write("<script language=javascript> alert('deleting') </script>");
        }
    }
结果发现点击GridView3上的删除,系统先执行的是pageload再执行RowDeleting,是这样的吗??? 但是我修改的一个页面上面,我写这样的代码,有时候删除了2条记录,而测试页上面没有问题

解决方案 »

  1.   

    if (!IsPostBack)
            {
    SqlDataSource2.SelectCommand = "select * From t_94";
    SqlDataSource2.DeleteCommand = "delete from t_94 where id = @id";
    GridView3.DataSource = SqlDataSource2;GridView3.DataBind();
    }提交到服务器时,先进pageload,不过pageload之前还有些事件
      

  2.   

    肯定是先Page_Load,在控件事件,所以一般Page_Load绑定数据会这样写
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                SqlDataSource2.SelectCommand = "select * From t_94";
                SqlDataSource2.DeleteCommand = "delete from t_94 where id = @id";
                GridView3.DataSource = SqlDataSource2;            GridView3.DataBind();
                Response.Write("<script language=javascript> alert('load') </script>");
               }
            }
      

  3.   

    加上IsPostBack 这样就不会出现重新绑定,因为重新绑定后值和事件就丢了
      

  4.   

    Ctoyun(林莉) ( ) 要提问就多上线,每天给10分,可以累加
      

  5.   

    我在onload里面这样写了:
    if(!IsPostBack)
                {
                SqlDataSource2.SelectCommand = "select * From t_94";
                SqlDataSource2.DeleteCommand = "delete from t_94 where id = @id";
                GridView3.DataSource = SqlDataSource2;            GridView3.DataBind();
                Response.Write("<script language=javascript> alert('load') </script>");
               }
    可是在gridview的rowdeleting里面删除的时候就提示DeleteCommand 里面没删除语句,可是onload里面明明有啊,而且在deleting里面又加一遍后,提示说connectstring属性尚未初始化。
      

  6.   

    于是我把SqlDataSource2的connectstring和deletecommand/selectcommand都又赋值了一边,现在可以删除了,但是gridview里面的却还在(后台删除,页面还有)
      

  7.   

    if (!IsPostBack) 加了再说
      

  8.   

    按钮事件里删除完了再绑定一次gridview或者if (!IsPostBack)里面写绑定代码
      

  9.   

    IsPostBack是Page类的一个属性,返回值为一个布尔值。一般放在Page_Load事件中。当页面是第一次打开时其值为False,若当前页面为一个提交后的页面其值为True.?通俗的说就是当你的叶面第一次登录时会调用if(!IsPostBack){}中的语句,而在此页面未被关闭之前再次进入该页面时(例如,用back<-返回该页面则{}中的语句不会再被调用;系统的说是是否将页面再重新回传给web服务器!函数作用是获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问。
    记得有些邦定必须放在这里面,否则会出一些莫名的错误?当每次点击服务器端按钮,都会对Page类重构
    调用Page_Load事件,然后才会调用响应事件,譬如说页面提交等等
    放在
     if (!IsPostBack) {
            // Validate initially to force the asterisks
          // to appear before the first roundtrip.
            Validate();
        }
    是为了判断是否是第一次调用此页面IsPostBack是指是否第一次调用这个页面。
    假如是只需要执行一次的一些页面上的设置,可以放在这个里面。有加快速度的功能。?在page_load 中
    要每次刷新页面都执行的代码写在
    If IsPostBack() Then Exit Sub
    之上
    只执行一次的,写在它的下面
      

  10.   

    搞定了,哈哈
    wuxing2006(金宝)
    Lattejoe(苏蓝)
    到:
    http://community.csdn.net/Expert/topic/5584/5584759.xml?temp=.9057733
    http://community.csdn.net/Expert/topic/5581/5581792.xml?temp=.9534418
    http://community.csdn.net/Expert/topic/5586/5586710.xml?temp=.5984613
    http://community.csdn.net/Expert/topic/5583/5583862.xml?temp=.6996576
    去接分,你们给我的提示最大