[研究][ASP.NET]ListView客製化分頁顯示DataPager控制項(二)
2022-04-06
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#
此篇為這篇的改良
[研究][ASP.NET]ListView客製化分頁顯示DataPager控制項(一)
https://shaurong.blogspot.com/2022/04/aspnetlistviewpager.html
ListView 中部份內容,DataPager可不放 ListView 中,若放 ListView 外,要加上 PagedControlID;另外可以設定多個 DataPager 指向同一個ListView,不管是在 ListView 內或外,都同時可以顯示和作用。
Default.aspx
<asp:DataPager ID="DataPager1" PagedControlID="ListView1" runat="server" PageSize="10">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="True" />
<asp:NumericPagerField PreviousPageText="上10頁" ButtonCount="10" NextPageText="下10頁" ButtonType="Button" />
<asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="True" ShowPreviousPageButton="False" />
<asp:TemplatePagerField OnPagerCommand="TemplatePagerField_OnPagerCommand" >
<PagerTemplate>
<%--<td class="info">--%>
目前在第 <b>
<%# Container.TotalRowCount > 0 ? Math.Ceiling(((double)(Container.StartRowIndex + Container.MaximumRows) / Container.MaximumRows)) : 0 %>
</b>頁,共 <b>
<%# Math.Ceiling((double)Container.TotalRowCount / Container.MaximumRows)%>
</b>頁(共 <%# Container.TotalRowCount %> 筆)
<%--</td>--%>
跳到第<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# Container.TotalRowCount > 0 ? Math.Ceiling(((double)(Container.StartRowIndex + Container.MaximumRows) / Container.MaximumRows)) : 0 %>" Width="50px"></asp:TextBox>頁
<%--<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" ></asp:LinkButton> --%>
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" />
</PagerTemplate>
</asp:TemplatePagerField>
</Fields>
</asp:DataPager>
|
Go 按鈕用
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" ></asp:LinkButton>
或下面都可以
<asp:Button ID="Button1" runat="server" CausesValidation="False" CommandArgument="-1" CommandName="Page" Text="GO" />
註:用 Buuton 在某些情況好像會出錯,若發生,請改回佣 LinkButton。
********************************************************************************
Default.aspx.cs
using System;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TemplatePagerField_OnPagerCommand(object sender, DataPagerCommandEventArgs e)
{
// Check which button raised the event
switch (e.CommandName)
{
//case "Next":
// int newIndex = e.Item.Pager.StartRowIndex + e.Item.Pager.PageSize;
// if (newIndex <= e.TotalRowCount)
// {
// e.NewStartRowIndex = newIndex;
// e.NewMaximumRows = e.Item.Pager.MaximumRows;
// }
// break;
//case "Previous":
// e.NewStartRowIndex = e.Item.Pager.StartRowIndex - e.Item.Pager.PageSize;
// e.NewMaximumRows = e.Item.Pager.MaximumRows;
// break;
//case "First":
// e.NewStartRowIndex = 0;
// e.NewMaximumRows = e.Item.Pager.MaximumRows;
// break;
case "Page":
TextBox txtNewPageIndex = (TextBox)e.Item.FindControl("txtNewPageIndex");
int startRowIndex = 0;
int startPageIndex = 0;
//string startRowIndexStr = "";
string startPageIndexStr = "";
if (txtNewPageIndex != null)
{
startPageIndexStr = txtNewPageIndex.Text;
Int32.TryParse(startPageIndexStr, out startPageIndex);
}
e.NewStartRowIndex = (startPageIndex - 1) * e.Item.Pager.PageSize;
if (e.NewStartRowIndex < 0)
e.NewStartRowIndex = 0;
if (e.NewStartRowIndex > e.TotalRowCount)
e.NewStartRowIndex = (e.TotalRowCount/ e.Item.Pager.PageSize)
* e.Item.Pager.PageSize;
e.NewMaximumRows = e.Item.Pager.MaximumRows;
//DataPager1.SetPageProperties(e.NewStartRowIndex, e.NewMaximumRows, true);
break;
}
}
}
}
|
(完)
相關
[研究][ASP.NET]ListView客製化分頁顯示DataPager控制項(三)
https://shaurong.blogspot.com/2022/04/aspnetlistviewdatapager_8.html
[研究][ASP.NET]ListView客製化分頁顯示DataPager控制項(二)
https://shaurong.blogspot.com/2022/04/aspnetlistviewdatapager.html
[研究][ASP.NET]ListView客製化分頁顯示DataPager控制項(一)
https://shaurong.blogspot.com/2022/04/aspnetlistviewpager.html
[研究][ASP.NET]設定 Gridview 分頁樣式為:第X頁,共X頁,第一頁 上一頁 下一頁 最後一頁 跳到第X頁 每頁顯示X筆
https://shaurong.blogspot.com/2020/04/aspnet-gridview-xx-x-x.html

沒有留言:
張貼留言