WebForm1.aspx:
<%@ Register TagPrefix="webdiyer" Namespace="Wuqi.Webdiyer" Assembly="AspNetPager" %>
<TABLE id="Table1" style="HEIGHT: 448px" cellSpacing="0" cellPadding="0" width="550" align="center"
border="0">
<TR>
<TD style="HEIGHT: 35px" align="center" width="150" bgColor="#6699cc"><FONT face="宋体">id</FONT></TD>
<TD style="HEIGHT: 35px" align="center" width="400" bgColor="#6699cc"><FONT face="宋体">class_name</FONT></TD>
</TR>
<TR>
<TD vAlign="top" align="center" colSpan="2"><asp:datalist id="DL" runat="server" Width="550px" Height="240px">
<AlternatingItemStyle BackColor="#FFE0C0"></AlternatingItemStyle>
<ItemTemplate>
<TABLE id="Table2" cellSpacing="0" cellPadding="0" width="300" border="0">
<TR>
<TD align="center" width="150"><%#DataBinder.Eval(Container.DataItem,"ID")%></TD>
<TD align="center" width="400"><%#DataBinder.Eval(Container.DataItem,"Class_name")%></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:datalist></TD>
</TR>
<TR>
<TD align="right" colSpan="2"><webdiyer:aspnetpager id="Pager" runat="server" PageSize="8" ShowCustomInfoSection="Left" PagingButtonSpacing="3px"
ShowInputBox="Always" SubmitButtonText="转到" PageChanging="Pager_PageChanging"></webdiyer:aspnetpager></TD>
</TR>
</TABLE>
WebForm1.aspx.cs:
private readonly string SQLCONNECTION =ConfigurationSettings.AppSettings["SQLCONNECTION"];
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection myconnection = new SqlConnection(SQLCONNECTION);
string sql ="Select count(*) from Middle_class";
SqlCommand mycommand = new SqlCommand(sql,myconnection);
myconnection.Open();
Pager.RecordCount= Convert.ToInt32(mycommand.ExecuteScalar());
myconnection.Close();
BindDL();
}
}
private void BindDL()
{
SqlConnection myconnection = new SqlConnection(SQLCONNECTION);
string sql ="select ID,class_name from Middle_class";
SqlDataAdapter da = new SqlDataAdapter(sql,myconnection);
DataSet ds =new DataSet();
myconnection.Open();
da.Fill(ds,Pager.PageSize*(Pager.CurrentPageIndex - 1),Pager.PageSize,"middle_class");
myconnection.Close();
DL.DataSource =ds;
DL.DataBind();
//动态设置用户自定义文本内容
Pager.CustomInfoHTML ="记录总数:<font color=/"blue/"><b>"+Pager.RecordCount.ToString()+"</b></font>";
Pager.CustomInfoHTML +=" 总页数:<font color=/"blue/"><b>"+Pager.PageCount.ToString()+"</b></font>";
Pager.CustomInfoHTML +=" 当前页:<font color=/"red/"><b>"+Pager.CurrentPageIndex.ToString()+"</b></font>";
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Pager.PageChanging += new Wuqi.Webdiyer.PageChangingEventHandler(this.Pager_PageChanging);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected void Pager_PageChanging(object src,PageChangingEventArgs e)
{
Pager.CurrentPageIndex = e.NewPageIndex;
BindDL();
}