DataList 的自定义分页






2.58/5 (15投票s)
2006年9月13日

130423
DataList 的自定义分页
引言
在这篇文章中,我将解释一种为 DataList 或 Repeater 提供自定义分页的方法。
如你所知,DataList 是一个非常强大的控件,但有一个缺点,它没有内置的分页功能,这是 DataGrid 提供的功能。为了为 DataList 或 Repeater 提供分页,我们可以使用 System.Web.UI.WebControls 命名空间中的“PagedDataSource”类进行自动分页,就像 DataGrid 一样,或者实现自定义分页功能。
所以这里是场景,我有一个包含每页 8 张图片的图片库。我需要提供分页,以便用户可以浏览和查看所有图片。第一步是创建 DataList 和分页链接。
//DataList
<asp:DataList runat="server" id="dlGallery"
RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="<%#DataBinder.Eval(Container.DataItem,"Image_URL")%>" width="90"
Height="90">
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
//Next/Prev Links.
<table border="0" width="410">
<tr>
<td align="left"><asp:LinkButton ID="lbtnPrev"
Runat="server">Prev</asp:LinkButton></td>
<td align="right"><asp:LinkButton ID="lbtnNext"
Runat="server">Next</asp:LinkButton></td>
</tr>
</table>
//代码
创建一个公共函数,该函数将只返回必要的行(分页后)。为此,我们需要五个静态变量。
private int imgID; private string imgTitle; private string imgURL; private static int pageSize = 8; //(This one will hold the no of records return //i mean "no. of records per page"). private static int pageIndex = 0; //(This one is for checking the current page). public DataSet GetAllImagesCustom(int pageIndex, out int outPageIndex) { try { int count = 0; DataSet ds = new DataSet(); ds = //retrieve the data from the database. //for paging int page = 0; //checking the whether the pageIndex value is not <First and >Last. //And if it is then assigning the default //values for pageIndex and page variables. if(((pageIndex-1) <= (ds.Tables[0].Rows.Count/pageSize)) && (pageIndex-1) >= 0) { //If the pageIndex is >=first and =<last then assigning the start position //eg. if pageIndex = 2 then value of 'page' = 8. //So in the loop it will add rows to the table //from the 8 th row. page = pageSize * (pageIndex-1); } else { //Assigning default values. page = 0; pageIndex = 1; } //creating a data table for adding the required rows or u //can clone the existing table. DataTable dtImg = new DataTable("Images"); DataColumn newCol = new DataColumn("Image_ID",Type.GetType("System.Int32")); dtImg.Columns.Add(newCol);//For storing image id. newCol = new DataColumn("Image_Title",Type.GetType("System.String")); dtImg.Columns.Add(newCol);//For storing image Title. newCol = new DataColumn("Image_URL",Type.GetType("System.String")); dtImg.Columns.Add(newCol);//For storing image URL. //adding the required rows to the datatable dtImg. foreach(DataRow nRow in ds.Tables[0].Rows) { //if the page=8 and pageIndex =2 then //rows between 8 to 16(if exists) will be added to the new table. if(count >= page && count < (pageSize * pageIndex)) { //Adding rows to the datatable 'dtImg'. dtImg.Rows.Add(nRow.ItemArray); } count++; } outPageIndex = pageIndex; return ds; } } catch(Exception ex) { throw ex; } } public void BindList() { ..... DataSet ds = new DataSet(); ds = GetAllImagesCustom(Convert.ToInt32(txtPageIndex.Text), out outPageIndex); dlGallery.DataSource = ds; dlGallery.DataBind(); //Assigning the new pageIndex value returned from the //function to the Hidden textbox. txtPageIndex.Text = Convert.ToString(outPageIndex); }
现在我们有一个每页 8 张图片的 DataList。但我们仍然没有完成导航部分。正如你从上面的函数中看到的,这很简单,不需要太多的逻辑。我们只需要将 pageindex 值加或减,然后调用 BindList 函数。
private void lbtnPrev_Click(object sender, System.EventArgs e) { //Actual pageIndex -1 txtPageIndex.Text = Convert.ToString(Convert.ToInt32(txtPageIndex.Text) - 1); BindList(); } private void lbtnNext_Click(object sender, System.EventArgs e) { //Actual pageIndex +1 txtPageIndex.Text = Convert.ToString(Convert.ToInt32(txtPageIndex.Text) + 1); BindList(); }
完成了!