【原创】刚开始学.Net写的分页类

时间:2021-08-30 16:21:08
菜菜灰的共享的分页类,使用非常简单,试合初学者使用,但效率较低,使用的是PagedDataSource分页

public  PagedDataSource GetPagedDataSource(DataSet MyDataSet, int  PageSize, int  PageNum, string  AspxName,Label PageLable)
        {
            PagedDataSource PDS
= new  PagedDataSource();
            
            PDS.AllowPaging
= true ;
            PDS.PageSize
= PageSize;
            PDS.DataSource
= MyDataSet.Tables[ 0 ].DefaultView;

            
int  CurrentPage = 0 ;
            
if (Convert.ToString(PageNum) != null )
            {
                CurrentPage
= int .Parse(PageNum.ToString());
            }
            
else
            {
                CurrentPage
= 1 ;
            }
            PDS.CurrentPageIndex
= CurrentPage - 1 ;

            
int  PageCount = PDS.PageCount;

            
string  FirstPage = (PageNum == 1 ) ? ( " <font face=\ " webdings\ "  style=\ " color:# 999 ;font - size:12px  ! important\ " >9</font> " ):( " <a href= " + AspxName + " page=1><font face=\ " webdings\ "  style=\ " font - size:12px  ! important\ " >9</font></a> " );
            
string  PrevPage = (PageNum == 1 ) ? ( " <font face=\ " webdings\ "  style=\ " color:# 999 ;font - size:12px  ! important\ " >7</font> " ):( " <a href= " + AspxName + " page= " + (PageNum - 1 ) + " ><font face=\ " webdings\ "  style=\ " font - size:12px  ! important\ " >7</font></a> " );
            
string  NextPage = (PageNum == (PageCount)) ? ( " <font face=\ " webdings\ "  style=\ " color:# 999 ;font - size:12px  ! important\ " >8</font> " ):( " <a href= " + AspxName + " page= " + (PageNum + 1 ) + " ><font face=\ " webdings\ "  style=\ " font - size:12px  ! important\ " >8</font></a> " );
            
string  LastPage = (PageNum == (PageCount)) ? ( " <font face=\ " webdings\ "  style=\ " color:# 999 ;font - size:12px  ! important\ " >:</font> " ):( " <a href= " + AspxName + " page= " + PageCount + " ><font face=\ " webdings\ "  style=\ " font - size:12px  ! important\ " >:</font></a> " );


            PageLable.Text
= " <span style=\ " float :right; " +Show+ " \ " >总记录:&nbsp; " + PDS.DataSourceCount + " &nbsp;&nbsp;&nbsp;每页:&nbsp; " + PageSize + " &nbsp;&nbsp;&nbsp;总页数:&nbsp; " + PageCount + " </span><span style=\ " float :left\ " > " + FirstPage + " &nbsp;&nbsp; " + PrevPage + " &nbsp;&nbsp; " + NextPage + " &nbsp;&nbsp; " + LastPage + " </span> " ;


            
return  PDS;
        }

调用示例

// PagedDataSource分页

string  MySql = " Select * from C_Company " ;
int  PageNum = (Request.QueryString[ " Page " ] == null ) ? ( 1 ):(Convert.ToInt32(Request.QueryString[ " Page " ]));
MyDataSet
= MyData.GetDataSet(MySql, 0 , 0 , " Table " );
PagedDataSource PDS
= new  PagedDataSource();
PDS
= MyData.GetPagedDataSource(MyDataSet, 10 ,PageNum,Request.CurrentExecutionFilePath + " ? " ,Label1);
this .DataGrid1.DataSource = PDS;
this .DataGrid1.DataBind();