datalist分页

自己写了个datalist的分页 先暂时放这里 有的地方还要修改  呵呵


static int pagecount;
static int pagesize=9;
static int currentpage;
private void GetData()
    
{
        DataTable dt 
= new DataTable();
        SqlConnection con 
= new SqlConnection(str);
        SqlDataAdapter da 
= new SqlDataAdapter("select * from authors",con);
        
try
        
{
            da.Fill(dt);
            
if (dt != null && dt.Rows.Count != 0)
            
{
                Session[
"dt"= dt;
                DataTable newtb 
= new DataTable();
                DataColumn col1 
= new DataColumn("au_id");
                DataColumn col2 
= new DataColumn("au_lname");
                newtb.Columns.Add(col1);
                newtb.Columns.Add(col2);

                
for (int i = 0; i < pagesize; i++)
                
{
                    DataRow row 
= newtb.NewRow();
                    row[
0= dt.Rows[i][0];
                    row[
1= dt.Rows[i][1];
                    newtb.Rows.Add(row);
                }

                DlTest.RepeatColumns 
= 3;
                DlTest.DataSource 
= newtb;
                DlTest.DataBind();
                pagecount 
= dt.Rows.Count / pagesize;
                
if (pagecount > 1)
                
{
                    currentpage 
= 1;
                    LinkLastPage.Visible 
= true;
                    LinkNextPage.Visible 
= true;
                }

            }

        }

        
catch
        

            
        }

    }

    
private void getPageData(int cpage)
    
{
        DataTable tb 
= new DataTable();
        tb 
= (DataTable)Session["dt"];
        DataTable newtb 
= new DataTable();
        DataColumn col1 
= new DataColumn("au_id");
        DataColumn col2 
= new DataColumn("au_lname");
        newtb.Columns.Add(col1);
        newtb.Columns.Add(col2);
        
for (int i = (cpage-1)*pagesize+1; i <= cpage * pagesize; i++)
        
{
            DataRow row 
= newtb.NewRow();
            row[
0= tb.Rows[i][0];
            row[
1= tb.Rows[i][1];
            newtb.Rows.Add(row);
        }

        DlTest.RepeatColumns 
= 3;
        DlTest.DataSource 
= newtb;
        DlTest.DataBind();
    }


    
protected void LinkLastPage_Click(object sender, EventArgs e)
    
{
        currentpage 
-= 1;
        
if (currentpage < 1)
        
{
            Response.Write(
"<script language='javascript'>alert('已是最前一页了!');</script>");
            currentpage 
= 1;
            
return;
        }

        getPageData(currentpage);
    }

    
protected void LinkNextPage_Click(object sender, EventArgs e)
    
{
        currentpage
+=1;
        
if (currentpage > pagecount)
        
{
            Response.Write(
"<script language='javascript'>alert('已是最后一页了!');</script>");
            currentpage 
= pagecount;
            
return;
        }

        getPageData(currentpage);
    }
原文地址:https://www.cnblogs.com/joy/p/726524.html