我做了一个基类,里面有GridView的一些公共属性,但是其他页面继承该类后,运行效果可以显示出部分数据(比如PageSize=20,就显示20条),但分页的数字不见了,没有1,2,3,4之类的数字可选择。
贴出部分代码,请高手指点。
//基类的部分代码
public class ClassBase:System.Web.UI.Page
{
    private GridView _GridView;
    protected GridView GridView
    {
        get { return _GridView; }
        set
        {
            _GridView = value;            
            _GridView.AllowPaging = true;
            _GridView.PagerSettings.Mode = PagerButtons.Numeric;             
            _GridView.AutoGenerateColumns = false;
            _GridView.PageSize = 20;
            _GridView.ShowFooter = true;
            _GridView.ShowHeader = true;
            _GridView.AllowSorting = true;
            _GridView.PageIndexChanging += new GridViewPageEventHandler(_GridView_PageIndexChanging);
        }
    }
    private DataTable _PageDataSource;
    protected DataTable PageDataSource
    {
        get { return _PageDataSource; }
        set
        {
            _PageDataSource = value;
            _GridView.DataSource = _PageDataSource;        }
    }    protected virtual void OnSetPageDataSource()
    {
        //
    }    protected void DropDownEvent()
    {
        //  _GridView.PageCount = -1;
        _GridView.PageIndex = 0;
        this.OnSetPageDataSource();
        _GridView.DataBind();
    }    protected void _GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        _GridView.PageIndex = e.NewPageIndex;
        this.OnSetPageDataSource();
        _GridView.DataBind();
    }
}////////子页面 部分代码
public partial class SubPage:ClassBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.GridView = GridView1;
        base.DropDownEvent();
    }    protected override void OnSetPageDataSource()
    {
        this.PageDataSource = GetData(base.GridView.PageIndex, base.GridView.PageSize);
    }
}

解决方案 »

  1.   

     PagedDataSource page = new PagedDataSource();                                  //构建分页类
                page.PageSize = Convert.ToInt32(this.TextBox4.Value);                          //设置显示的行数
                page.DataSource = dt.DefaultView;
                page.AllowPaging = true;                                                       //千万别忘记启动分页
                lblCount.Text = page.DataSourceCount.ToString();                               //获得共多少行
                lblPageCount.Text = page.PageCount.ToString();                                 //共多少页
                page.CurrentPageIndex = Convert.ToInt32(lblpageindex.Text.ToString()) - 1;     //指定当前页的索引
                GridView1 = Class1.CreateDataGridList(GridView1);
                GridView1.DataSource = page;                                                   //绑定到GridView1上面
                GridView1.DataBind();
     protected void btnup_Click(object sender, EventArgs e)
        {
            //当前页的索引减一
            lblpageindex.Text = Convert.ToString(Convert.ToInt32(lblpageindex.Text.ToString()) - 1);
            bind();                                 //绑定一下
        }  protected void btndown_Click(object sender, EventArgs e)
        {
            //当前页的索引加一
            lblpageindex.Text = Convert.ToString(Convert.ToInt32(lblpageindex.Text.ToString()) + 1);
            bind();                                 //绑定一下
        }protected void btnFirst_Click(object sender, EventArgs e)
        {
            //第一页
            lblpageindex.Text = "1";
            bind();                                 //绑定一下
        }protected void btnlast_Click(object sender, EventArgs e)
        {
            //当前页的索引减一
            bind();
            lblpageindex.Text = this.lblPageCount.Text;
            bind();                                 //绑定一下
        }
      

  2.   

    你的分頁是第3方控件還是gridview自帶的?
      

  3.   

    3楼的兄弟
    就是GridView控件,因为很多页面用到这控件,所以我做了一个基类ClassBase,把PageSize之类的属性放在一起,但是页面继承该类之后在GridView底部不能显示1,2,3,4之类的分页选项,asp1.1中DataGrid设置VirtualItemCount的值等于总行数就可以,asp2.0GridView我不知道怎么弄都不对,你可以看看我的代码,那里错了。
      

  4.   

    看了一下:
    好象PagerSettings.Position没设置,
    应该设置为Bottom
    楼主试试!!
      

  5.   

    补充:由于是类,对gridView应事无巨细都应该设置一遍!!
      

  6.   


    刚照你说的加了,没起作用。
    我这代码在asp.net1.1的DataGrid控件中是可以的,DataGrid控件有个VirtualItemCount属性,设置为表中的总记录数就会显示1,2,3,4的分页导航。
    在GridView中找不到VirtualItemCount属性了,怎么解决?
      

  7.   

    你的代码太多了,看不过来。如果使用数据源控件,上手很快,因为仅需要手写很少的代码,可以参看Scott Mitchell的例子
      

  8.   


    sp1234大哥你好。
    我整了个简化的代码如下,帮我看下。
     protected void Page_Load(object sender, EventArgs e)
        {
           PagedDataSource page = new PagedDataSource();
            page.VirtualCount = 60;//设置总数为60条
            page.PageSize = 20;//每页为20条
            page.AllowCustomPaging = true;
            page.AllowServerPaging = true;//启用自定义分页
            page.AllowPaging = true;//启用分页
            page.DataSource = GetData().DefaultView;
            page.CurrentPageIndex = 0; 
            GridView1.DataSource = page;  
            GridView1.DataBind();
        }
    照这个代码结果应该显示1,2,3共个分页啊,结果却只有一页20条数据,GridView的自定义分页怎么这么麻烦,没dataGrid好用
      

  9.   

    用过GridView的自定义分页的高手出来顶下哦。
      

  10.   

    我试了试楼主提供的代码:
    次序换一换,页号是能够显示的!
            PagedDataSource page = new PagedDataSource();
            page.DataSource = ds.Tables[0].DefaultView;        DataView dv = ds.Tables[0].DefaultView;
            GridView1.DataSource = page;
            GridView1.DataBind();

            page.VirtualCount = 60;//设置总数为60条 
             page.PageSize = 10;//每页为20条 
             page.AllowCustomPaging = true;
            page.AllowServerPaging =true;//启用自定义分页 
             page.AllowPaging = true;//启用分页 
             //page.DataSource = GetData().DefaultView;
            page.CurrentPageIndex = 0;楼主试试
      

  11.   

    上面代码多了一条:应该是以下:
            PagedDataSource page = new PagedDataSource();
            page.DataSource = ds.Tables[0].DefaultView;        GridView1.DataSource = page;
            GridView1.DataBind();
            page.VirtualCount = 60;//设置总数为60条 
             page.PageSize = 10;//每页为20条 
             page.AllowCustomPaging = true;
            page.AllowServerPaging =true;//启用自定义分页 
             page.AllowPaging = true;//启用分页 
             //page.DataSource = GetData().DefaultView;
            page.CurrentPageIndex = 0;
      

  12.   

    17楼的兄弟,你给的代码我刚试验了,不行。
    page.VirtualCount = 60;
    page.PageSize = 10
    的结果应该是6页,但是如果我的数据源只取了30条的话,你的代码就只显示3页,你的代码和GridView默认的分页一样了。
      

  13.   

    那个兄弟帮忙解决了,分全是他的哈,分不够还可以再加。小弟初学Asp.net2.0,以前弄Winform的
      

  14.   

    那个兄弟帮忙解决了,分全是他的哈,分不够还可以再加。小弟初学Asp.net2.0,以前弄Winform的
      

  15.   


    protected void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    this.nowpage.Text="1";
    bindinvit();
    }
    }
    void bindinvit()
    {
    int currentpage=Convert.ToInt32(this.nowpage.Text);//将当前页面的页面数复值给currentpage
    SqlConnection conn=new SqlConnection("server=.;database=web;uid=sa;pwd=");
    SqlDataAdapter asd=new SqlDataAdapter("select * from invit order by sendtime desc",conn);
    DataSet ds=new DataSet();
    asd.Fill(ds,"invit");
    System.Web.UI.WebControls.PagedDataSource pds=new PagedDataSource();
    pds.DataSource=ds.Tables["invit"].DefaultView;
    pds.AllowPaging=true;
    pds.PageSize=8;
    pds.CurrentPageIndex=currentpage-1;//当前页的索引号
    this.pagecount.Text=pds.PageCount.ToString();//总页数
    //
    if(currentpage==1)
    {
    this.LinkButton1.Enabled=false;//第一页
    this.LinkButton2.Enabled=false;//上一页
    }
    else
    {
    this.LinkButton1.Enabled=true;
    this.LinkButton2.Enabled=true;
    }
    if(currentpage==pds.PageCount)
    {
    this.LinkButton3.Enabled=false;//下一页
    this.LinkButton4.Enabled=false; //最后一页
    }
    else
    {
    this.LinkButton3.Enabled=true;
    this.LinkButton4.Enabled=true;
    }
    this.GridView.DataSource=pds;
    this.GridView.DataBind();
    }这是以前写的一个,自己看一下吧
      

  16.   

    我有一个分页的,很好使,要得可以给我发个邮件:[email protected]
      

  17.   

    弄了个ObjectDataSource控件,貌似能解决我的问题了,不过ObjectDataSource控件灵活性很差,还是asp.net1.1的DataGrid控件好用,它有VirtualItemCount属性,继续求更灵活的处理方法。