欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈VS中的DataPager分页

程序员文章站 2024-02-14 17:39:46
微软的datapager分页功能很强大,不要设置数据库存储过程,只要添加个datapager控件,关联下要分页的控件,简单设置就可以有不错的分页效果。当然要有更理想的效果还...

微软的datapager分页功能很强大,不要设置数据库存储过程,只要添加个datapager控件,关联下要分页的控件,简单设置就可以有不错的分页效果。当然要有更理想的效果还是要前台和后台处理下。

winform下的datapager 显示模式:

浅谈VS中的DataPager分页

webform下的样式由templatepagerfield,nextpreviouspagerfield和numericpagerfield控制

通过设置上面几个控件的配合也可以达到winform下的效果,这3个控件中最重要的是templatepagerfield控件。

下面简单看看templatepagerfield控件可以怎么设置:

复制代码 代码如下:

  <%@ page language="vb" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
    "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
  protected sub templatepagerfield_onpagercommand(byval sender as object, _
    byval e as datapagercommandeventargs)
    ' check which button raised the event
    select case e.commandname
      case "next"
        dim newindex as integer = e.item.pager.startrowindex + e.item.pager.pagesize
        if newindex <= e.totalrowcount then
          e.newstartrowindex = newindex
          e.newmaximumrows = e.item.pager.maximumrows
        end if
      case "previous"
        e.newstartrowindex = e.item.pager.startrowindex - e.item.pager.pagesize
        e.newmaximumrows = e.item.pager.maximumrows
      case "first"
        e.newstartrowindex = 0
        e.newmaximumrows = e.item.pager.maximumrows
    end select
  end sub
</script>
<html xmlns="" >
  <head id="head1" runat="server">
    <title>templatepagerfield.onpagercommand example</title>   
    <style type="text/css">
      body    
      {
          text-align: center;
          font: 12px arial, helvetica, sans-serif;
      }
      .item
      {
        border: solid 1px #2f4f4f;
        background: #e6e6fa;
      }
    </style>
  </head>
  <body>
    <form id="form1" runat="server">
      <h3>templatepagerfield.onpagercommand example</h3>
      <asp:listview id="storeslistview"
        datasourceid="storesdatasource"
        runat="server">
        <layouttemplate>
          <table width="350" runat="server" id="tblstore">
            <tr runat="server">
              <th runat="server">id</th>
              <th runat="server">store name</th>
            </tr>
            <tr id="itemplaceholder" runat="server">
            </tr>
          </table>
         </layouttemplate>
         <itemtemplate>
          <tr runat="server">
            <td class="item">
              <asp:label id="idlabel" runat="server" text='<%#eval("customerid") %>' />
            </td>           
            <td align="left" class="item">
              <asp:label id="namelabel" runat="server" text='<%#eval("name")%>' />
            </td>
          </tr>
        </itemtemplate>
      </asp:listview>
      <br />
      <asp:datapager runat="server"
        id="contactsdatapager"
        pagesize="30"
        pagedcontrolid="storeslistview">
        <fields>
          <asp:templatepagerfield onpagercommand="templatepagerfield_onpagercommand">
            <pagertemplate>
              <asp:linkbutton id="firstbutton" runat="server" commandname="first"
                text="<<" enabled='<%# container.startrowindex > 0 %>' />
              <asp:linkbutton id="previousbutton" runat="server" commandname="previous"
                text='<%# (container.startrowindex - container.pagesize + 1) & " - " & (container.startrowindex) %>'
                visible='<%# container.startrowindex > 0 %>' />
              <asp:label id="currentpagelabel" runat="server"
                text='<%# (container.startrowindex + 1) & "-" & (iif(container.startrowindex + container.pagesize > container.totalrowcount, container.totalrowcount, container.startrowindex + container.pagesize)) %>' />
              <asp:linkbutton id="nextbutton" runat="server" commandname="next"
                text='<%# (container.startrowindex + container.pagesize + 1) & " - " & (iif(container.startrowindex + container.pagesize*2 > container.totalrowcount, container.totalrowcount, container.startrowindex + container.pagesize*2)) %>'
                visible='<%# (container.startrowindex + container.pagesize) < container.totalrowcount %>' />
            </pagertemplate>
          </asp:templatepagerfield>
        </fields>
      </asp:datapager>    
      <asp:sqldatasource id="storesdatasource" runat="server"
            connectionstring="<%$ connectionstrings:adventureworks_dataconnectionstring %>"
            selectcommand="select [customerid], [name] from sales.store order by [name]">
      </asp:sqldatasource>
    </form>
  </body>
</html>