O exemplo de paginação abaixo foi construído com recortes de diversos sites e mostra um grid de paginação, acessando um banco de dados Access.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>
<script language="C#" runat="server">
void Page_Load( Object sender, EventArgs e ){
BindGrid();
}
void MyDataGrid_Page( Object sender, DataGridPageChangedEventArgs e){
int startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize;
BindGrid();
ShowStats();
}
void BindGrid(){
DataSet DS = new DataSet();
ADODataSetCommand MyCommand = new ADODataSetCommand( "select * from Links", "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" + Server.MapPath("data.mdb") );
MyCommand.FillDataSet( DS, "Links" );
MyDataGrid.DataSource = DS.Tables["Links"].DefaultView;
MyDataGrid.DataBind();
ShowStats();
}
void PagerButtonClick( Object sender, EventArgs e ){
string arg = ((LinkButton)sender).CommandArgument;
switch ( arg ){
case "next":
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) MyDataGrid.CurrentPageIndex += 1;
break;
case "prev":
if (MyDataGrid.CurrentPageIndex > 0) MyDataGrid.CurrentPageIndex -= 1;
break;
case "last":
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
// page number
MyDataGrid.CurrentPageIndex = arg.ToInt32();
}
BindGrid();
ShowStats();
}
void ShowStats(){
lblCurrentIndex.Text = "CurrentPageIndex is " + MyDataGrid.CurrentPageIndex;
lblPageCount.Text = "PageCount is " + MyDataGrid.PageCount;
}
</script>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
AllowPaging="True"
PageSize="5"
PageCount="1"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="MyDataGrid_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
/>
</form>
<p>
<asp:LinkButton id="btnFirst" runat="server"
Text="Go to the first page"
CommandArgument="0"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
<asp:LinkButton id="btnPrev" runat="server"
Text="Previous page"
CommandArgument="prev"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
<asp:LinkButton id="btnNext" runat="server"
Text="Next page"
CommandArgument="next"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
<asp:LinkButton id="btnLast" runat="server"
Text="Go to the last page"
CommandArgument="last"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
<p>
<table bgcolor="#eeeeee" cellpadding="6"><tr><td nowrap><font face="Verdana" size="-2">
<asp:Label id="lblCurrentIndex" runat="server" /><br>
<asp:Label id="lblPageCount" runat="server" /><br>
0 comentários:
Postar um comentário