分页的例子(吓搞了一个)

后台:

protected void Page_Load(object sender, System.EventArgs e)
  {
   if(!IsPostBack)
   {
    SqlConnection conn=db.cn();
    conn.Open();
    SqlCommand cmd=new SqlCommand();
    cmd.Connection=conn;
                cmd.CommandText = "SELECT COUNT(*) FROM gg WHERE (title LIKE '%是%')";
    ViewState["count"]=cmd.ExecuteScalar();
    
    SqlDataAdapter da=new SqlDataAdapter();
    DataSet ds=new DataSet();    

    int page=10;
    page=page*(Convert.ToInt32(Request.Params["page"])-1);
    if(page>0)
    {
                    cmd.CommandText = "SELECT TOP 10 * FROM gg WHERE (id NOT IN (SELECT TOP " + page + " id FROM gg WHERE (title LIKE '%是%'))) and (title LIKE '%是%')";
    }
    else
    {
     cmd.CommandText="SELECT TOP 11 * FROM gg";
    }
    da.SelectCommand=cmd;
    da.Fill(ds);
    PagedDataSource objPage=new PagedDataSource();

    objPage.DataSource=ds.Tables[0].DefaultView;

    objPage.AllowPaging=true;

    objPage.PageSize=10;

    int CurPage;
    if(Request.QueryString["Page"]!=null)
     CurPage=Convert.ToInt32(Request.QueryString["Page"]);
    else CurPage=1;
    objPage.CurrentPageIndex=CurPage-1;
    if(objPage.CurrentPageIndex%20==0)
    {
     Session["begin"]=objPage.CurrentPageIndex+1;
    }

    Label1.Text="当前页:第"+CurPage.ToString()+"页";
    if(!objPage.IsFirstPage)
     HyperLink1.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage-1);
    if(!objPage.IsLastPage)
     HyperLink2.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" +Convert.ToString(CurPage+1);
    DataList1.DataSource=ds;
    DataList1.DataBind();
    ds.Clear();
    
    conn.Close();
   }
  }

  public void GetString()
  {
            int i = Convert.ToInt32(Session["begin"]);
        if (Convert.ToInt32(Request.Params["page"]) % 20 == 0 && Request.Params["page"] != null)
        {
            i = i - 20;
        }
        if ( Convert.ToInt32(Request.Params["page"])>i + 20)
        {
            i = i+20;
        }
   for(;i <= (Convert.ToInt32(ViewState["count"])+10)/10;i++)
   {
                if (i%20 == 1)
                {
                    if (i == 1)
                    { }
                    else
                    {
                        Response.Write("</TD>&nbsp;<a href='" + Request.CurrentExecutionFilePath + "?page=" + (i - 20) + "' alt=上20页>上20页</a>&nbsp;</TD>");
                    }
                } 
    Response.Write("</TD>&nbsp;<a href='"+Request.CurrentExecutionFilePath+"?page="+i+"'>"+i+"</a>&nbsp;</TD>");
    if(i%20==0)
    {
     i++;
                    Response.Write("</TD>&nbsp;<a href='" + Request.CurrentExecutionFilePath + "?page=" + i + "' alt=下20页>下20页</a>&nbsp;</TD>");
     break;
    }
   }  
  }

前台:

<form id="Form1" method="post" runat="server">
   <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 40px; WIDTH: 704px; POSITION: absolute; TOP: 24px; HEIGHT: 163px"
    cellSpacing="1" cellPadding="1" width="704" border="1">
    <TR>
     <TD style="WIDTH: 63px"><FONT face="宋体"></FONT></TD>
     <TD style="WIDTH: 516px"><FONT face="宋体"><asp:datalist id="DataList1" runat="server">
        <ItemTemplate>
         <%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
        </ItemTemplate>
        <SeparatorTemplate>
         ......................
        </SeparatorTemplate>
        <AlternatingItemTemplate>
         <%#DataBinder.Eval(Container.DataItem,"title").ToString() %>
        </AlternatingItemTemplate>
       </asp:datalist></FONT></TD>
     <TD><FONT face="宋体"></FONT></TD>
    </TR>
    <TR>
     <TD style="WIDTH: 63px"><FONT face="宋体"><asp:hyperlink id="HyperLink1" runat="server">HyperLink</asp:hyperlink></FONT></TD>
     <TD style="WIDTH: 516px">
      <table>
       <%GetString();%>
      </table>
     </TD>
     <TD><FONT face="宋体"><asp:hyperlink id="HyperLink2" runat="server">HyperLink</asp:hyperlink><asp:label id="Label1" runat="server">当前第XXX页</asp:label></FONT></TD>
    </TR>
   </TABLE>
  </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值