当我按了网面上一个button按扭的时候重新生成的这个网页会不会在动行page_load这个方法
我有一点迷获

解决方案 »

  1.   

    所以如果想在提交后不做什么事的话就加上if(!this.IsPostBack)
    {
      //第一次才做的事。
    }
      

  2.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    DataSet ds=new DataSet();
    SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1);
    sa.Fill(ds,"wocaonima");
                DataRow dr=ds.Tables["wocaonima"].NewRow();
                dr["a"]=1;
    dr["b"]="8";
    dr["c"]="8";
    ds.Tables[0].Rows.Add(dr);
    //SqlCommandBuilder sb=new SqlCommandBuilder(sa);
                //sa.InsertCommand=sb.GetInsertCommand();
    //SqlCommand sc=new SqlCommand("delete wocao",sqlConnection1);
    //sa.InsertCommand=sc;
    //sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=PAN;packet size=4096;user id=sa;data source=PAN;persist security i" +
    "nfo=False;initial catalog=test";
    // 
    // sqlCommand1
    // 
    this.sqlCommand1.CommandText = "SELECT * FROM wocao";
    this.sqlCommand1.Connection = this.sqlConnection1;
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    DataSet ds=new DataSet();
    SqlDataAdapter sa=new SqlDataAdapter("SELECT * FROM wocao",sqlConnection1);
    sa.Fill(ds,"wocaonima");
    DataRow dr=ds.Tables["wocaonima"].NewRow();
    dr["a"]=1;
    dr["b"]="8";
    dr["c"]="8";
    ds.Tables[0].Rows.Add(dr);
    SqlCommandBuilder sb=new SqlCommandBuilder(sa);
    sa.InsertCommand=sb.GetInsertCommand();
    sa.Update(ds,"wocaonima"); DataGrid1.DataSource=ds;
    DataGrid1.DataBind();

    }
      

  3.   

    1.有什么问题?
    2.把绑定datagrid的程序做一个方法,可多次调用。
      

  4.   

    把Page_Load函数里的代码,全放到if(!ispostback)里面,再去执行一下,效果应该有所不同啦
      

  5.   

    if(!this.IsPostBack)
    {
      放这里边
    }页面每次回发都会执行Page_load事件.
      

  6.   

    if(!Page.IsPostBack)
    {
        内容;
    }
    以后里面的内容就不会每次提交都执行了
      

  7.   

    不管你点button, 还是dropdownlist(如果autopostback=true)等,都会首先执行page_load,也就是说,只要进入页面必执行page_load,可以用if(!IsPostBakc){...},楼上说了.
      

  8.   

    Click the link to solve your problem.Good luck!