在ASP.NET里,我把绑定数据代码写在if(!Page.IsPostBack)下,DATAGRID却不刷新显示增加的数据,写在外边可以显示。怎么回事?

解决方案 »

  1.   

    if(!Page.IsPostBack)的作用你知道吗?
      

  2.   

    !IsPostBack是指在页面第一次加载时执行.像在页面里点了按钮之后的操作,是控件引起的操作,不属于!IsPostBack的范畴,所以不会引起绑定操作.写在外面,就等于任何状况下都执行这个绑定操作,自然能看到了.
      

  3.   

    我把代码贴上来讨论一下。我声明一下,在if(!Page.IsPostBack)里和在if(!Page.IsPostBack)外,绑定数据的操作都执行了。
    文件1:datagrid显示界面 部分代码<script language="javascript">
    function openwin()
    {
    var ret;
    ret = window.showModalDialog('AddBaseInfon_Group.aspx?aid=1',window,'dialogHeight:200px;dialogWidth:316px;center:Yes;Help:No;Resizable:No;Status:no;Scroll:no;Status:no;');
    window.document.Form1.submit();

    }
    .cs文件 private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    this.bnAdd .Attributes["onclick"]="javascript:return openwin()";
    this.bnDel.Attributes.Add("OnClick","return confirm('你确认要删除所选择的群组吗?');");
    if(!Page.IsPostBack)
    {
    BindData();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.bnAdd.Click += new System.EventHandler(this.bnAdd_Click);
    this.bnDel.Click += new System.EventHandler(this.bnDel_Click);
    this.lbBM.Click += new System.EventHandler(this.lbBM_Click);
    this.lbJS.Click += new System.EventHandler(this.lbJS_Click);
    this.lbFY.Click += new System.EventHandler(this.lbFY_Click);
    this.lbXM.Click += new System.EventHandler(this.lbXM_Click);
    this.bngo.Click += new System.EventHandler(this.bngo_Click);
    this.txb_ItemPerPage.TextChanged += new System.EventHandler(this.txb_ItemPerPage_TextChanged);
    this.bnfirst.Click += new System.EventHandler(this.PagerButton_Click);
    this.bnpre.Click += new System.EventHandler(this.PagerButton_Click);
    this.bnnext.Click += new System.EventHandler(this.PagerButton_Click);
    this.bnlast.Click += new System.EventHandler(this.PagerButton_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void BindData()
    {
    SqlDataReader dr=null;
    BaseInfo bi=new BaseInfo();
    dr=bi.GetGroupInfo(-1);
    DataTable dt=Tools.ConvertDataReaderToDataTable(dr);
    int ICount=dgQZList.PageSize-(dt.Rows.Count % dgQZList.PageSize);
    for(int i=0;i<ICount;i++)
    {
    dt.Rows.Add(dt.NewRow());
    }
    dgQZList.DataSource =null;
    dgQZList.DataSource = dt.DefaultView;
    dgQZList.DataBind();
    dr.Close();
    if(this.dgQZList.CurrentPageIndex ==0)
    {
    this.bnfirst.Enabled =false;
    this.bnpre.Enabled=false;
    }
    else
    {
    this.bnfirst.Enabled=true;
    this.bnpre.Enabled =true;
    }
    if(dgQZList.CurrentPageIndex==dgQZList.PageCount-1)
    {
    SetDGNewRow(dgQZList.PageSize,ICount);
    this.bnnext.Enabled =false;
    this.bnlast.Enabled =false;
    }
    else
    {
    this.bnnext.Enabled =true;
    this.bnlast.Enabled =true;
    }
    bi=null;
    this.lbl_totalrecord.Text =dgQZList.PageCount.ToString();
    lbl_curpage.Text = txb_PageNo.Text = (dgQZList.CurrentPageIndex + 1).ToString();
    txb_ItemPerPage.Text = dgQZList.PageSize.ToString();
    lbl_totalpage.Text = dgQZList.PageCount.ToString();
    }
      

  4.   

    文件2:数据添加界面 private void SaveData()
    {
    BaseInfo bi=new BaseInfo();
    switch(AID)
    {
    case 1:
    QZID=bi.AddGroup(txtMC.Text,txtQM.Text);
    if(QZID>0)
    {
    Response.Write("<script>alert('群组信息保存成功!');window.close();window.dialogArguments.location='BaseInfonListView_Group.aspx?tmpid="+DateTime.Now.Ticks.ToString()+"';</script>");
    }
    break;
    case 2:
    int RetVal=bi.UpdateGroup(QZID,txtMC.Text,txtQM.Text);
    if(RetVal>0)
    {
    Response.Write("<script>alert('群组信息更新成功!');window.close();window.dialogArguments.location='BaseInfonListView_Group.aspx?tmpid="+DateTime.Now.Ticks.ToString()+"';</script>");
    }
    break;
    }
    bi=null;
    }
      

  5.   

    ff167(没有) ( ) 信誉:100
    !IsPostBack是指在页面第一次加载时执行.像在页面里点了按钮之后的操作,是控件引起的操作,不属于!IsPostBack的范畴,所以不会引起绑定操作.写在外面,就等于任何状况下都执行这个绑定操作,自然能看到了. 你回答错问题了,哈哈!楼主问的不是这个
      

  6.   

    在添加数据的方法程序结尾重新绑定一下DATAGRID就可以了。
      

  7.   

    添加数据是在新页面执行的。关闭后在已经重新调用了绑定函数,我调试程序了,提取的数据表里的数据正确,绑定datagrid后没有显示出来。
      

  8.   

    if(!Page.IsPostBack)里面当然不能够写了,这个是判断是否重新载入了页面呀!
      

  9.   

    把if(!Page.IsPostBack)去掉之后,就可以达到你的那中效果了
      

  10.   

    添加数据是在新页面执行的。关闭后在已经重新调用了绑定函数,我调试程序了,提取的数据表里的数据正确,绑定datagrid后没有显示出来。
    那你得在添加完数据后从新把DATAGRID 刷新一遍。
      

  11.   

    我都这样做了,还是不行,
    dgQZList.DataSource =null;
    dgQZList.DataSource = dt.DefaultView;
    dgQZList.DataBind();
    楼上一位,你说的刷新怎么刷新啊?
      

  12.   

    如果在page_load里去掉if(!Page.IsPostBack),下面的代码执行就会有问题,查找不到控件的ID。
    private void bnDel_Click(object sender, System.EventArgs e)
    {
    string SelStr=this.GetSelectedItemID("qzid");
    if(SelStr!="")
    {
    BaseInfo bi=new BaseInfo();
    bi.DeleteGroup(SelStr);
    Response.Write("<script language=javascript>alert('所选群组删除成功!');</script>");
    BindData();
    }
    else
    {
    Response.Write("<script language=javascript>alert('请选择要删除的群组!');</script>");
    }
    }
    private string GetSelectedItemID(string controlID)
    {
    string selectedID;
    selectedID = "";
    foreach (DataGridItem item in this.dgQZList.Items)
    {
    if(((CheckBox)item.FindControl(controlID)).Checked==true )
    selectedID += dgQZList.DataKeys[item.ItemIndex] + ",";
    }
    if(selectedID.Length>0)
    selectedID=selectedID.Substring(0,selectedID.Length-1);
    return selectedID;
    }
      

  13.   

    那不清楚了,按说执行插入成功后重新绑定DATAGRID是可以的
    有没有高手解决一下LZ的问题?