日期:2009-09-17  浏览次数:20391 次

ASP.NET分页组件学与用——使用篇



大家好,这篇文章承启上一篇文章《ASP.NET分页组件学与用——教学篇》。



在《ASP.NET分页组件学与用——教学篇》中,我们讲解了分页组件的整个创建过程,那么在这一篇文章中,我们主要讨论一下如何使用该组件。



请按以下步骤操作:

1、 创建一个WEB应用程序工程。



2、 将上篇文章中生成的组件添加到工具箱中。如果您不知道添加的过程,请参考文章《ASP.NET组件编程step by step》





3、 在默认的WEB窗体页面中,切换到HTML视图,把下列HTML代码复制到<form>标签中。

<div align="center">

<center>

<table border="0" cellpadding="0" cellspacing="0" width="756" height="155" style="FONT-SIZE:12px">

<tr>

<td width="756" height="23">分页控件的使用实例</td>

</tr>

<tr>

<td width="756" height="110" valign="top" align="center" runat="server" id="myHTML"></td>

</tr>

<tr>

<td width="756" height="22">

</td>

</tr>

</table>

</center>

</div>



4、 该HTML代码生成一个三行一列的表格,从工具箱将控件拖到该表格的第三行中。



5、 在private void Page_Load(object sender, System.EventArgs e)事件处理程序中添加如下代码:

int cp;

if(this.Request.Params["currentPage"] == null)

{

cp = 1;

}

else

{

cp = Convert.ToInt32(this.Request.Params["currentPage"]);

}



SqlConnection con = new SqlConnection("server=accp-lzh;uid=sa;pwd=sasa;database=Northwind");

SqlCommand cmd = new SqlCommand("select * from [Order Details]",con);

SqlDataAdapter adapter = new SqlDataAdapter();

adapter.SelectCommand = cmd;

DataSet ds = new DataSet();

con.Open();

adapter.Fill(ds,"table");

con.Close();



DataTable dt = new DataTable();

dt = ds.Tables["table"];

//以下设置控件参数

this.LzhPages1.AllCount = dt.Rows.Count;//总记录条数

this.LzhPages1.CurrentPage = cp;//当前页,从URL中读取

this.LzhPages1.ShowPages = 10;//导航数字个数

this.LzhPages1.Count = 10;//每页显示的记录条数



StringBuilder sql = new StringBuilder();

//以下三个字符串最终组合成一个完整的表格<table>

string HTMLHeader = "";//<table>标签

string HTMLBody = "";//中间有若干个<tr>标签,即行

string HTMLFoot = "";//</table>标签



sql.AppendFormat("select top {0} * from [Order Details] where 1=1 and OrderID not in (select top {1} OrderID from [Order Details] where