怎么在VS中利用DataPager实现一个分页功能

发布时间:2021-01-13 16:36:38 作者:Leah
来源:亿速云 阅读:175

怎么在VS中利用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="http://www.w3.org/1999/xhtml" >
  <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">
      <h4>TemplatePagerField.OnPagerCommand Example</h4>
      <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>

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 怎么在Django中利用类实现一个分页功能
  2. 怎么在iOS中利用AutoLayout实现分页滚动功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

datapager vs

上一篇:Application在ASP.NET 中的作用是什么

下一篇:怎么在.NET中使用repeater实现一个分页效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》