Repeater 使用 AspNetPager分页,在网上查资料用了我很长时间,但觉得都不太好而且用到自己的项目中都有错误,这个是转的别人发表的
DB.cs文件代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
/// <summary>
///DB 的摘要说明
/// </summary>
public class DB
{
public DB()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection creaton()
{
SqlConnection con = null;
try
{
con = new SqlConnection("server=limingfei;database=limingfei;uid=sa;pwd=sa;");
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return con;
}
}
cs文件下代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
SqlConnection conn = DB.creaton();
conn.Open();
SqlCommand Count = new SqlCommand();
Count.Connection = conn;
Count.CommandText = "select count(*) from [user]";
AspNetPager1.RecordCount = (int)Count.ExecuteScalar();
conn.Close();
BindData();
}
}
public void BindData()
{
SqlConnection conn = DB.creaton();
string sql = "select * from [user] order by ID desc";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "10");
DataTable dt = ds.Tables["10"];
Repeater1.DataSource =dt;
Repeater1.DataBind();
}
protected void AspNetPager1_PageChanged1(object sender, EventArgs e)
{
BindData();
}
}
aspx文件代码:
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:limingfeiConnectionString %>"
SelectCommand="SELECT [userName], [category], [releaseName], [PicAddress], [neirong] FROM [user]">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate><center>
<table width="1002" border="1" >
<tr>
<td width="150px" height="93px">
<img src='<%# eval_r("PicAddress")%>' alt="哈哈全是美女呀!!!" width="150px" height="93px" />
</td>
<td width="802px">
<table>
<tr>
<td>
名称:<asp:Label ID="Label1" runat="server" Text='<%# eval_r("userName")%>' />
类别:<asp:Label ID="Label2" runat="server" Text='<%# eval_r("category")%>' />
</td>
</tr>
<tr>
<td>
内容:<asp:Label ID="Label3" runat="server" Text='<%# eval_r("neirong")%>' />
</td>
</tr>
<tr>
<td>
发布者:<asp:Label ID="Label4" runat="server" Text='<%# eval_r("releaseName")%>' />
</td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<table width="1002" border="1">
<td>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" CurrentPageButtonPosition="Center"
FirstPageText="首页" LastPageText="尾页" NextPageText="后页" PrevPageText="前页" OnPageChanged="AspNetPager1_PageChanged1">
</webdiyer:AspNetPager>
</td>
</table>
</center>
</form>
</body>
</html>