GridView 分頁 版面
放置在GridView Html 將GridView 加入RowCreated
<PagerTemplate>
<div class="List_file"></div>
<div class="Feed">
<div class="pagination sabrosus">
<table align="center" width="100%">
<tr>
<td style="text-align: center">
<%--第<b><asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label></b>頁--%>
共<b><asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label></b>頁
<asp:LinkButton ID="lbnFirst" runat="Server" CommandArgument="First" CommandName="Page"
Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" Text="首頁"></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" CommandArgument="Prev" CommandName="Page"
Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" Text="上一頁"></asp:LinkButton>
<%-- 在此加入 PlaceHolder --%>
<asp:PlaceHolder ID="NumbericPagerPlaceHolder" runat="server"></asp:PlaceHolder>
<asp:LinkButton ID="lbnNext" runat="Server" CommandArgument="Next" CommandName="Page"
Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>"
Text="下一頁"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" CommandArgument="Last" CommandName="Page"
Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>"
Text="尾頁"></asp:LinkButton>
</td>
</tr>
</table>
</div>
</div>
</PagerTemplate>
.CS檔
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow currentRow = e.Row;
if (currentRow.RowType == DataControlRowType.Pager)
{
PlaceHolder numbericPagerPlaceHoder = ((PlaceHolder)currentRow.FindControl("NumbericPagerPlaceHolder"));
LinkButton numbericButton = null;
int pageSize = GridView1.PageSize;
int pageIndex = GridView1.PageIndex;
int pageCount = GridView1.PageCount;
int startIndex = (pageIndex >= pageCount - 1) ? 0 : (pageIndex < 5) ? 0 : (pageIndex > pageCount - 5) ? pageCount - 10 : pageIndex - 5;
int endIndex = (startIndex + 9 <= pageCount - 1) ? startIndex + 9 : pageCount - 1;
numbericPagerPlaceHoder.Controls.Add(new LiteralControl(" "));
for (int i = startIndex; i <= endIndex; i++)
{
if (i == pageIndex)
{
numbericPagerPlaceHoder.Controls.Add(
new LiteralControl(String.Format("<span style='color: deeppink; font-weight: bold;'>{0}</span> ", i + 1)));
}
else
{
numbericButton = new LinkButton();
numbericButton.Text = (i + 1).ToString();
numbericButton.CommandName = "Page";
numbericButton.CommandArgument = (i + 1).ToString();
numbericPagerPlaceHoder.Controls.Add(numbericButton);
numbericPagerPlaceHoder.Controls.Add(new LiteralControl(" "));
}
}
numbericPagerPlaceHoder.Controls.Add(new LiteralControl(" "));
}
}
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁