Copy codeThe Code is as follows:
Note: encapsulate data binding controls (such as System. Web. UI. WebControls. DataGrid, System. Web. UI. WebControls. GridView, System. Web. UI. WebControls. DetailsView)
// And System. Web. UI. WebControls. FormView. This class cannot be inherited.
DataList
Public DataTable GetDataSet (string SQL)
{
SqlConnection conn = this. getconn ();
SqlDataAdapter sdr = new SqlDataAdapter (SQL, conn );
DataSet rs = new DataSet ();
Sdr. Fill (rs );
Return rs. Tables [0];
}
Public PagedDataSource PageDataListBind (string SQL, int currentPage, int PageSize)
{
PagedDataSource PPS = new PagedDataSource ();
PPS. DataSource = GetDataSet (SQL). DefaultView;
PPS. AllowPaging = true;
PPS. PageSize = PageSize;
Pds. CurrentPageIndex = currentPage-1;
Return PPS;
}
DB db = new DB ();
PagedDataSource PPS = new PagedDataSource ();
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind ();
}
}
Public void bind ()
{
Pds = db. PageDataListBind ("select * from tb_word", Convert. ToInt32 (lblCurrentPage. Text), 2 );
LnkBtnFirst. Enabled = true;
LnkBtnLast. Enabled = true;
LnkBtnPrevious. Enabled = true;
LnkBtnNext. Enabled = true;
If (lblCurrentPage. Text = "1 ")
{
LnkBtnFirst. Enabled = false;
LnkBtnPrevious. Enabled = false;
}
If (lblCurrentPage. Text = maid. PageCount. ToString ())
{
LnkBtnLast. Enabled = false;
LnkBtnNext. Enabled = false;
}
LblSumPage. Text = PPS. PageCount. ToString ();
DataList1.DataSource = pds;
DataList1.DataKeyField = "ID ";
DataList1.DataBind ();
}
Protected void lnkBtnFirst_Click (object sender, EventArgs e)
{
LblCurrentPage. Text = "1 ";
Bind ();
}
Protected void lnkbtnprevius_click (object sender, EventArgs e)
{
LblCurrentPage. Text = (Convert. ToInt32 (lblCurrentPage. Text)-1). ToString ();
Bind ();
}
Protected void lnkBtnNext_Click (object sender, EventArgs e)
{
LblCurrentPage. Text = (Convert. ToInt32 (lblCurrentPage. Text) + 1). ToString ();
Bind ();
}
Protected void lnkBtnLast_Click (object sender, EventArgs e)
{
LblCurrentPage. Text = lblSumPage. Text;
Bind ();
}
<Form id = "form1" runat = "server">
<Div>
<Asp: DataList ID = "DataList1" runat = "server">
<ItemTemplate>
<Table border = "1">
<Tr>
<Td> ID </td> <td> title </td> <td> content </td>
</Tr>
<Tr>
<Td> <% # Eval ("ID") %> </td>
<Td> <% # Eval ("Title") %> </td>
<Td> <% # Eval ("Content") %> </td>
</Tr>
</Table>
</ItemTemplate>
</Asp: DataList>
The current page number is [<asp: Label ID = "lblCurrentPage" runat = "server" Text = "1"> </asp: Label>]
Total page number [<asp: Label
ID = "lblSumPage" runat = "server" Text = "0"> </asp: Label>] Page
<Asp: LinkButton ID = "lnkBtnFirst" runat = "server" Font-Underline = "False" OnClick = "lnkBtnFirst_Click"> page 1 </asp: LinkButton>
<Asp: LinkButton ID = "lnkBtnPrevious" runat = "server" Font-Underline = "False" OnClick = "lnkbtnprevius_click"> previous page </asp: LinkButton>
<Asp: LinkButton ID = "lnkBtnNext" runat = "server" Font-Underline = "False" OnClick = "lnkBtnNext_Click"> next page </asp: LinkButton>
<Asp: LinkButton ID = "lnkBtnLast" runat = "server" Font-Underline = "False" OnClick = "lnkBtnLast_Click"> last page </asp: LinkButton>
</Div>
</Form>