Use PageDataSource paging implementation code in Asp.net

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.