The asp.net Datalist control implements the paging function.

Source: Internet
Author: User

Code on the. aspx page Copy codeThe Code is as follows: <asp: dataList ID = "DataList1" runat = "server" Width = "976px" Height = "745px" BorderWidth = "2px" CellPadding = "2" CellSpacing = "2" RepeatColumns = "7 "RepeatDirection =" Vertical "BorderColor =" #666666 ">
<ItemTemplate>
<Table>
<Tr>
<Td>
<A href = '<% # "Show. aspx? Iname = "+ Eval (" iname ") %> '> 'width = "120" alt = "click to view a large image"/> </a>
</Td>
</Tr>
</Table>
</ItemTemplate>
</Asp: DataList> <asp: SqlDataSource ID = "sqlperformance1" runat = "server" ConnectionString = "<% $ ConnectionStrings: s601ConnectionString %> "SelectCommand =" SELECT [iname] FROM [Image] "> </asp: SqlDataSource>
<Table style = "width: 976px" cellpadding = "0" cellspacing = "0">
<Tr>
<Td style = "width: 976px; text-align: right; font-size: 12pt; height: 25px;">
<Asp: Label ID = "Label7" runat = "server" Text = "Current page number:"> </asp: Label>
[
<Asp: Label ID = "labPage" runat = "server" Text = "1"> </asp: Label>
]
<Asp: Label ID = "Label6" runat = "server" Text = "total page number:"> </asp: Label>
[
<Asp: Label ID = "labBackPage" runat = "server"> </asp: Label>
] <Asp: LinkButton ID = "lnkbtnOne" runat = "server" Font-Underline = "False" ForeColor = "Red"
OnClick = "lnkbtnOne_Click"> page 1 </asp: LinkButton>
<Asp: LinkButton ID = "lnkbtnUp" runat = "server" Font-Underline = "False" ForeColor = "Red"
OnClick = "lnkbtnUp_Click"> previous page </asp: LinkButton>
<Asp: LinkButton ID = "lnkbtnNext" runat = "server" Font-Underline = "False" ForeColor = "Red"
OnClick = "lnkbtnNext_Click"> next page </asp: LinkButton>
<Asp: LinkButton ID = "lnkbtnBack" runat = "server" Font-Underline = "False" ForeColor = "Red"
OnClick = "lnkbtnBack_Click"> last page </asp: LinkButton> </td>
</Tr>
</Table>

On the. aspx. cs pageCopy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
DlBind ();

}
Public void dlBind ()
{
SqlConnection sqlcon;
String strCon = "Data Source = (local); Database = S601; Uid = sa; Pwd = 123456 ";
Int curpage = Convert. ToInt32 (this. labPage. Text );
PagedDataSource ps = new PagedDataSource ();
Sqlcon = new SqlConnection (strCon );
Sqlcon. Open ();
String sqlstr = "SELECT [iname], iid FROM [Image]";
SqlDataAdapter MyAdapter = new SqlDataAdapter (sqlstr, sqlcon );
DataSet ds = new DataSet ();
MyAdapter. Fill (ds, "iid ");
Ps. DataSource = ds. Tables ["iid"]. DefaultView;
Ps. AllowPaging = true; // whether pagination is allowed
Ps. PageSize = 35; // displayed quantity
Ps. CurrentPageIndex = curpage-1; // obtain the page number of the current page
This. lnkbtnUp. Enabled = true;
This. lnkbtnNext. Enabled = true;
This. lnkbtnBack. Enabled = true;
This. lnkbtnOne. Enabled = true;
If (curpage = 1)
{
This. lnkbtnOne. Enabled = false; // The first page button is not displayed.
This. lnkbtnUp. Enabled = false; // the previous page button is not displayed.
}
If (curpage = ps. PageCount)
{
This. lnkbtnNext. Enabled = false; // the next page is not displayed.
This. lnkbtnBack. Enabled = false; // The last page is not displayed.
}
This. labBackPage. Text = Convert. ToString (ps. PageCount );
This. DataList1.DataSource = ps;
This. DataList1.DataKeyField = "iid ";
This. DataList1.DataBind ();
Sqlcon. Close ();
}
Protected void lnkbtnOne_Click (object sender, EventArgs e)
{
This. labPage. Text = "1 ";
This. dlBind ();
}
Protected void lnkbtnUp_Click (object sender, EventArgs e)
{
This. labPage. Text = Convert. ToString (Convert. ToInt32 (this. labPage. Text)-1 );
This. dlBind ();
}
Protected void lnkbtnNext_Click (object sender, EventArgs e)
{
This. labPage. Text = Convert. ToString (Convert. ToInt32 (this. labPage. Text) + 1 );
This. dlBind ();
}
Protected void lnkbtnBack_Click (object sender, EventArgs e)
{
This. labPage. Text = this. labBackPage. Text;
This. dlBind ();
}

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.