GridView paging style design
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
...... >
<Columns>
<asp:BoundField DataField="Name" HeaderText="姓名" SortExpression="Name" />
<asp:BoundField DataField="OrgID" HeaderText="單位編號" SortExpression="OrgID" />
.............
</Columns>
<PagerTemplate>
<div style="text-align:center;">
<asp:LinkButton ID="btnFirstPage" runat="server" CommandArgument="First" CommandName="Page" Text="First"
Visible='<%# (Container.NamingContainer as GridView).PageIndex > 0 %>' />
<asp:LinkButton ID="btnPrevPage" runat="server" CommandArgument="Prev" CommandName="Page" Text="Pre"
Visible='<%# (Container.NamingContainer as GridView).PageIndex > 0 %>' />
<asp:LinkButton ID="btnP4" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex-3 > 0 %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex-3 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex-3 %>' />
<asp:LinkButton ID="btnP3" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex-2 > 0 %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex-2 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex-2 %>' />
<asp:LinkButton ID="btnP2" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex-1 > 0 %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex-1 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex-1 %>' />
<asp:LinkButton ID="btnP1" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex > 0 %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex %>' />
<asp:LinkButton ID="btnCurrent" runat="server"
Enabled="false"
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex+1 %>' />
<asp:LinkButton ID="btnN1" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+1 < (Container.NamingContainer as GridView).PageCount %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex+2 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex+2 %>' />
<asp:LinkButton ID="btnN2" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+2 < (Container.NamingContainer as GridView).PageCount %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex+3 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex+3 %>' />
<asp:LinkButton ID="btnN3" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+3 < (Container.NamingContainer as GridView).PageCount %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex+4 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex+4 %>' />
<asp:LinkButton ID="btnN4" runat="server"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+4 < (Container.NamingContainer as GridView).PageCount %>'
CommandArgument='<%# (Container.NamingContainer as GridView).PageIndex+5 %>'
CommandName="Page" Text='<%# (Container.NamingContainer as GridView).PageIndex+5 %>' />
<asp:LinkButton ID="btnNextPage" runat="server" CommandArgument="Next" CommandName="Page" Text="Next"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+1 < (Container.NamingContainer as GridView).PageCount %>' />
<asp:LinkButton ID="btnLastPage" runat="server" CommandArgument="Last" CommandName="Page" Text="Last"
Visible='<%# (Container.NamingContainer as GridView).PageIndex+1 < (Container.NamingContainer as GridView).PageCount %>' />
,共<%# (Container.NamingContainer as GridView).PageCount %>頁,
到第<asp:TextBox ID="tbPageIndex" runat="server" Text='<%# (Container.NamingContainer as GridView).PageIndex+1 %>' Columns="3" />
<asp:LinkButton ID="btnGotoPage" runat="server" Text="Go" OnClick="GotoPage_Click"
OnClientClick="__doPostBack($(this).attr('id').split('_').join('$'),$(this).parent().find('input:text').val());return false;" />
</div>
</PagerTemplate>
</asp:GridView>
protected void GotoPage_Click(object sender, EventArgs e)
{
GridView gv = (sender as LinkButton).NamingContainer.NamingContainer as GridView;
try
{
// __doPostBack 參數
int p = int.Parse(Request.Form["__EVENTARGUMENT"]);
if (p < 1 || p > gv.PageCount)
{
ClientScript.RegisterClientScriptBlock(
GetType(),
"f",
"alert('錯誤頁碼!');",
true
);
}
else
{
gv.PageIndex = p - 1;
gv.DataBind();
}
}
catch
{
ClientScript.RegisterClientScriptBlock(
GetType(),
"f",
"alert('錯誤頁碼!');",
true
);
}
}