这个控件有人说在2.0下面不能使用,试了一下,看来说法有误哦!仍然可以使用,方法也一样。下面把方法说明一下。
1.将AspNetPager控件放入工具箱的方法是右键点击工具箱,选择添加项目,然后刘览相关dll文件。
2.控件外观的设定
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" UrlPaging="true" PageSize="5" ShowCustomInfoSection="Left" NumericButtonTextFormatString="[{0}]" ShowBoxThreshold="5" AlwaysShow="true" OnPageChanged="AspNetPager1_PageChanged" >
</webdiyer:AspNetPager>
其实,一些属性我也不懂是什么,ShowCustomInfoSection大约是一个安放自定义文本的东东。PageSize设定分页显示的记录笔数。OnPageChanged事件调用后台的方法。
3.设定总的记录笔数在Page_Load事件里面
this.AspNetPager1.RecordCount = pager.GetAuthorsRowsCount("ahthors");
这里计算记录总笔数的方法是:
/// <summary>
/// 通用方法用于计算记录笔数
/// </summary>
/// <param name="mySql"></param>
/// <returns></returns>
public int ExecuteCount(string mySql)
{
SqlCommand myCmd = new SqlCommand(mySql, myConn);
myCmd.CommandText = mySql;
try
{
myConn.Open();
return (int)myCmd.ExecuteScalar();
}
catch (Exception ex)
{
return -99;
}
finally
{
myCmd.Dispose();
myConn.Close();
}
}
/// <summary>
/// 得到当前记录的笔数
/// </summary>
/// <param name="tablename"></param>
/// <returns></returns>
public int GetAuthorsRowsCount(string tablename)
{
string sql = "select count(*) from authors";
return this.ExecuteCount(sql);
}
3,将部分数据插入数据集,并绑定到DATAGRID中。
通用方法:
/// <summary>
/// 得到数据集用于分页的方法
/// </summary>
/// <param name="mySql">要执行的查询语句</param>
/// <param name="reapterstr1">从哪一笔数据开始插入数据</param>
/// <param name="reapterstr2">共插入多少笔数据</param>
/// <param name="myTable">给插入数据集中的表命名</param>
/// <returns>数据集</returns>
public DataSet ExecuteSqlDsReapter(string mySql, int reapterstr1, int reapterstr2, string myTable)
{
SqlCommand myCmd = new SqlCommand(mySql, myConn);
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
DataSet dsReapter = new DataSet();
try
{
myDa.Fill(dsReapter, reapterstr1, reapterstr2, myTable);
return dsReapter;
}
catch (Exception ex)
{
return new DataSet();
}
finally
{
myDa.Dispose();
myConn.Close();
}
}
得到当前数据集
/// <summary>
/// 得到数据集
/// </summary>
/// <param name="table">给填充到数据集的表命名</param>
/// <param name="repeater1">从哪一笔记录开始插入数据</param>
/// <param name="repeaterstr2">共插入几笔数据</param>
/// <returns></returns>
public DataSet GetAuthorsRows(string table,int repeater1,int repeaterstr2)
{
string sql = "select * from authors";
return this.ExecuteSqlDsReapter(sql, repeater1, repeaterstr2, table);
}
绑定到DATAGRID ,这里只是举DATAGRID例,GRIDVIEW我没试过。
/// <summary>
/// 有两个任务:绑定数据集;显示记录信息
/// </summary>
public void DataBindChannel()
{
//绑定数据集
DataSet list = new DataSet();
int repeater1 = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1);
int repeater2 = AspNetPager1.PageSize;
list = pager.GetAuthorsRows("authours", repeater1, repeater2);
dg.DataSource = list.Tables["authours"];
dg.DataBind();
//显示记录信息
AspNetPager1.CustomInfoText = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoText += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoText += " 当前页:<font color=/"red/"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
分页的方法
/// <summary>
/// 点分页按钮时调用的方法
/// </summary>
/// <param name="src"></param>
/// <param name="e"></param>
protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
DataBindChannel();
}