写一绑定数据方法,绑定数据时,gridview和AspNetPager都重新绑定
参考
http://www.cnblogs.com/lgxjt168/archive/2008/12/12/1353785.html

解决方案 »

  1.   

    用aspnetpager的url分页方式,在page_load中不用绑定Data_Bind()只要给aspnetpager控件的recordcount赋值就可以,用radiobuttonlist的话可以在SelectedIndexChanged事件中跳转本页如:Response.Redirect("XXX.aspx?type=RadioBUttonList.selectValue")之类的代码,只要在page_load和Data_Bind()判断好就不会出错
      

  2.   

    我会绑定gridview和aspnetpager,只是现在ral每改变一次选项的话,那gridview就要重新绑定,而aspnetpager怎么重新绑定呢?每次绑定的方法都不一样的,因为每次改变后数据源会不一样.在SelectedIndexChanged事件中我可以通过判断哪个被选种,之后重新绑定gridview,但是这时候怎么重新绑定aspnetpager呢?在AspNetPager1_PageChanged事件我现在代码如下:
            protected void AspNetPager1_PageChanged(object sender, EventArgs e)
            {
                Data_Bind();
            }
    这里Data_Bind()是第一次加载页面默认绑定的数据源,如下:
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    Data_Bind();
                }
            }
    但是比如我现在选中了radiobuttonlist的一项,比如是:未审核的文章
            protected void RadlContent_SelectedIndexChanged(object sender, EventArgs e)
            {
                int counts = 0;
                switch (RadlContent.SelectedValue)
                { 
                    case "0":
                        Data_Bind1(); //注意这里,绑定方法已经变了,不是Data_Bind()了
                        break;
             ....
           }
         }
    我是这样写的代码,这个时候选中未审核一项,gridview会重新绑定数据,但是protected void AspNetPager1_PageChanged 这个事件里还是绑定的刚打开页面的时候的方法呢,这个事件里该怎么判断并做出相应的绑定呢?
      

  3.   

    我这有个 AspNetPager 完整例子例子  你看下有用吗 if (!Page.IsPostBack)
            {
                myConnection.Open();
                com = new SqlCommand();
                com.Connection = myConnection;
                com.CommandText = "select count(*) from CG_Admin";
                AspNetPager1.AlwaysShow = true;
                AspNetPager1.PageSize = 10;
                AspNetPager1.RecordCount = (int)com.ExecuteScalar();            myConnection.Close();
                bind_Admin();        }  public void bind_Admin()
        {        String cmdText = "SELECT id,admin,isadmin=case isadmin when 1 then '超级管理员' when 0 then '普通管理员' end from CG_Admin";
            dr = new SqlDataAdapter(cmdText, myConnection);
            ds = new DataSet();
            dr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "CG_Admin");
            Show_Admin.DataSource = ds.Tables["CG_Admin"];
            Show_Admin.DataBind();     
            
        }
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;        bind_Admin();
        }
      

  4.   

    GridView与AspNetPager的帮定在同一函数中。
      

  5.   

    恩,就是因为要在同一函数中所以才出现这个问题了,第一次加栽页面绑定的函数是获取所有文章的,而当我选中 未审核 后,绑定的就应该是未审核的文章列表,所以这里是两个绑定函数,在选中 未审核 后,aspnetpager的事件里也应该相应的绑定获取 未审核 文章的函数,而页面第一次加载aspnetpager默认绑定的是获取 所有文章 的函数,我想问的就是这种情况下,aspnetpager事件里应该怎么判断,什么时候绑定哪个函数?
      

  6.   

    发一个用dropdownlist的例子    protected void Page_Load(object sender, EventArgs e)
        {;
            if (!IsPostBack)
            {
                if (ReqIsPass() != "")
                {
                    AspNetPager1.RecordCount = bllEx.Re_nCount("IsPass=" + ReqIsPass());
                    ddl_choose.SelectedValue = ReqIsPass();
                }
                else
                {
                    AspNetPager1.RecordCount = bllEx.Re_nCount("IsPass=1");
                    //modEx.IsPass = 1;
                }
                if (AspNetPager1.RecordCount == 0)
                {
                    errmsg = "暂无申请!";
                    AspNetPager1.Visible = false;
                    AspNetPager2.Visible = false;
                }
                else
                {
                    AspNetPager1.Visible = true;
                    AspNetPager2.Visible = true;
                }
                //BindView();
            }
        }    //接收关键字
        public string ReqIsPass()
        {
            usql remove = new usql();
            string IsPass = "";
            if (Request.QueryString.Count != 0)
            {
                if (Request.QueryString["ispass"] != null)
                {
                    IsPass = remove.unsql(Request.QueryString["ispass"].ToString());
                }
            }
            else { IsPass = ""; }        return IsPass;
        }    protected void ddl_choose_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("ExchangeManage.aspx?ispass=" + this.ddl_choose.SelectedValue);
        }    public void BindView()
        {
            if (ddl_choose.SelectedValue != "1")
            {
                this.gvProduct.Columns[4].Visible = false;
            }
            else
            {
                this.gvProduct.Columns[4].Visible = true;
            }
            if (ddl_choose.SelectedValue != "2")
            {
                this.gvProduct.Columns[5].Visible = false;
            }
            else
            {
                this.gvProduct.Columns[5].Visible = true;
            }
            ds = bllEx.SelExInfo(ddl_choose.SelectedValue,AspNetPager1.StartRecordIndex-1,AspNetPager1.PageSize);
            this.gvProduct.DataSource = ds;
            this.gvProduct.DataBind();
            ds.Clear();
            ds.Dispose();
        }    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindView();
        }
      

  7.   

    这个分页控件是别人写的,存在版权等问题,LZ有时间的话,完全可以自己写个控件。AspNetPager的用法你可以在google搜下,一大堆的东西。
      

  8.   

    AspNetPager1_PageChanged(object sender,eventargs e)
    {
    bind( anp1.pagesize,anp1.pageindex,strwhere)
    }
      

  9.   


     protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                    DataBind();
            }
            public override void DataBind()
            {
                Brand br = new Brand();
                int pageSiz = this.AspNetPager1.PageSize;
                int pageIndex = this.AspNetPager1.CurrentPageIndex;
                int pageCount = 0;
                int counts = 0;            this.rp1.DataSource = br.GetList(pageSiz, pageIndex, "", out pageCount, out counts);            this.AspNetPager1.RecordCount = counts;
                this.AspNetPager1.DataBind();
                this.AspNetPager2.RecordCount = counts;
                this.AspNetPager2.DataBind();
                this.rp1.DataBind();            base.DataBind();
            }        protected void AspNetPager1_PageChanged(object src, Bda.Common.Web.UI.WebControls.WebPageChangedEventArgs e)
            {
                AspNetPager1.CurrentPageIndex = e.NewPageIndex;
                AspNetPager2.CurrentPageIndex = e.NewPageIndex;
                DataBind();
            }
      

  10.   

    写一个独立的方法绑定数据,类似于楼上那样,不过获取数据时还要加上radiobuttonlist那个当前选中项的条件就行了,Page_Load、RadioButtonList的SelectedIndexChanged事件处理程序及AspNetPager的PageChanged事件处理程序都只需要调用该方法即可,另外在RadioButtonList的SelectedIndexChanged事件处理程序中在调用这个方法之后最好把AspNetPager的CurrentPageIndex设为1。
      

  11.   

    public partial class AspNetPager : System.Web.UI.Page
    {
        DataSet ds;
        SqlDataAdapter dr;
        SqlCommand com;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();
                string strconn = "Data Source=(local);Database=INFO;Uid=sa;Pwd=5354";//连接数据库
                SqlConnection con = new SqlConnection(strconn);
                con.Open();
                com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "select count(*) from zhong";//查询表
                AspNetPager1.AlwaysShow = true;
                //AspNetPager1.PageSize = 5;
                AspNetPager1.RecordCount = (int)com.ExecuteScalar();
                con.Close();
                RepeaterDataBind();
            }
        }    private void RepeaterDataBind()
        {
            //string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();
            string strconn = "Data Source=(local);Database=INFO;Uid=sa;Pwd=5354";//连接数据库
            dr = new SqlDataAdapter("select * from zhong", strconn);
            ds = new DataSet();
            dr.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "zhong");
            this.GridView1.DataSource = ds.Tables["zhong"];
            this.GridView1.DataBind();        AspNetPager1.CustomInfoHTML = " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
            AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
        }    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            RepeaterDataBind();
        }
        //protected string FormatString_Size_13(string str)
        //{//这里的代码可以不使用
        //    if (str.Length > 33)
        //    {
        //        str = str.Substring(0, 32) + "";
        //    }
        //    return str;
        //}
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {//分页发生改变时触发事件
            RepeaterDataBind();
        }
    }