在repeater、datalist控件中使用分页功能

<%@Import namespace="System.Data"%>

<%@Import namespace="System.Data.SqlClient"%>

<html>

<head>

<title>default</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<script language="C#" runat="server">

public void Page_Load(Object src,EventArgs e)

         {

SqlConnection cnn=new SqlConnection("server=zzl;uid=sa;pwd=970480;database=zzl");//连接数据库

SqlDataAdapter mycommand=new SqlDataAdapter("select * from start1",cnn);

//数据操作DataSet ds=new DataSet();

              mycommand.Fill(ds);      //实例dataset对象为ds,并把数据填充到ds

 

              PagedDataSource pds=new PagedDataSource();//对分页功能的类实例对象

              pds.DataSource=ds.Tables[0].DefaultView;//把数据赋予对象pp

              pds.AllowPaging=true;//允许进行分页

              pds.PageSize=6;//设置每页数据的个数

              int cpage;  //这个整数用来分析分页页数的

              if(Request.QueryString["page"]!=null) //这个判断语句的作用是对cpage进行赋值

                   cpage=Convert.ToInt32(Request.QueryString["page"]);

              else

                   cpage=1;

              pds.CurrentPageIndex=cpage-1;

//pp对象的当前引索值,因为引索值是从0开始,cpage1开始所以要减1

              if (!pds.IsFirstPage)

//Request.CurrentExecutionFilePath为当前的程序的文件名,直接写也可以

pre.NavigateUrl=Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(cpage-1);

 

              if (!pds.IsLastPage)

next.NavigateUrl=Request.CurrentExecutionFilePath+ "?page=" + Convert.ToString(cpage+1);

          

              repeater1.DataSource=pds;

              repeater1.DataBind();

         }

</script>

</head>

       <body>

              <table width="100%" border="0">

                     <tr>

                            <TD>&nbsp;&nbsp;<asp:label id="current" runat="server"/></TD>

                     </tr>

                     <tr>

                            <td>&nbsp;<asp:hyperlink id="pre" runat="server"><<</asp:hyperlink>

                                   <asp:hyperlink id="next" runat="server">>></asp:hyperlink>

                            </td>

                     </tr>

              </table>

              <asp:repeater id="repeater1" runat="server">

                     <itemtemplate>

                            <table width="100%" border="0">

                                   <tr>

                                          <td>&nbsp;&nbsp;<%#DataBinder.Eval(Container.DataItem,"product")%></td>

                                   </tr>

                                   <tr>

                                          <td>&nbsp;&nbsp;</td>

                                   </tr>

                            </table>

                     </itemtemplate>

              </asp:repeater>

       </body>

</html>

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

在repeater、datalist控件中使用分页功能

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭