AspNetPager是一个基于ASP.NET的分页控件,可以用于各种类型的Web应用程序中。它可以极大地简化分页过程,并且提供了很多用户可以配置和个性化的高级选项。
一、控件使用方式
1.引用命名空间
```csharp
using Wuqi.Webdiyer;
```
2.在ASP.NET Web Forms的页面上放置控件,并配置相关属性
```html
CurrentPageButtonCssClass="webdiyer-current-page" OnPageChanging="AspNetPager1_PageChanging">
```
ASP.NET Web Forms的后台代码如下:
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
int pageNo = 1;
int.TryParse(Request.QueryString["PageNo"], out pageNo);
var articleList = GetArticleList(pageNo, 10);
rptArticleList.DataSource = articleList;
rptArticleList.DataBind();
AspNetPager1.RecordCount = GetArticleCount();
}
private Listhtml
<%# Eval("Name") %>
CurrentPageButtonCssClass="webdiyer-current-page" OnPageChanging="AspNetPager1_PageChanging">
```
ASP.NET Web Forms的后台代码如下:
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
int pageNo = 1;
int.TryParse(Request.QueryString["PageNo"], out pageNo);
var productlist = GetProductList(pageNo, 9);
dlProductList.DataSource = productlist;
dlProductList.DataBind();
AspNetPager1.RecordCount = GetProductCount();
}
private List { List string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string queryString = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY ID DESC) AS ROWNUM, * FROM Products) AS P WHERE ROWNUM BETWEEN (@PageNo - 1) * @PageSize + 1 AND @PageNo * @PageSize"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Parameters.AddWithValue("@PageNo", pageIndex); command.Parameters.AddWithValue("@PageSize", pageSize); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Product product = new Product(); product.Id = (int)reader["Id"]; product.Name = reader["Name"].ToString(); product.Price = (decimal)reader["Price"]; product.ImagePath = reader["ImagePath"].ToString(); productList.Add(product); } reader.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } return productList; } private int GetProductCount() { int count = 0; string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; string queryString = "SELECT COUNT(*) FROM Products"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); try { connection.Open(); count = (int)command.ExecuteScalar(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } return count; } protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e) { Response.Redirect("Products.aspx?PageNo=" + e.NewPageIndex); } ``` 四、总结 AspNetPager控件是一个非常实用的分页控件,可以帮助我们快速完成分页功能的开发。在应用过程中,我们还可以根据自己的需求,个性化设定控件的各种属性,以达到更好的用户体验和使用效果。 壹涵网络我们是一家专注于网站建设、企业营销、网站关键词排名、AI内容生成、新媒体营销和短视频营销等业务的公司。我们拥有一支优秀的团队,专门致力于为客户提供优质的服务。 我们致力于为客户提供一站式的互联网营销服务,帮助客户在激烈的市场竞争中获得更大的优势和发展机会!
发表评论 取消回复