DataList Web 伺服器控件以某種格式顯示資料,這種格式可以使用模闆和樣式進行定義。DataList 控件可用于任何重複結構中的資料,如表。DataList 控件可以以不同的布局顯示行,如按列或行對資料進行排序。DataList 控件不能自動利用資料源控件的更新功能以及自動分頁或排序。若要使用 DataList 控件執行更新、分頁和排序,必須在編寫的代碼中執行更新任務。
在PetShop4.0中,有一個叫做CustomList的類引起了我們的注意,它繼承了DataList類,并對其進行了改寫,實作了分頁功能,然而,PetShop4.0中對分頁功能較簡單(隻有上下翻頁功能),本文介紹PetShop4.0中DataList類的改寫思路,并對其進行進一步的研究。
一、效果圖
這個效果圖比較粗糙,讀者可以根據自己的需要,對其進行改寫。
二、具體改寫的代碼
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomList ...{
public class CustomList : DataList ...{
//Static constants
private string HTML1 = "<table cellpadding=0 cellspacing=0 ><tr><td colspan=13>";
protected const string HTML2 = "</td></tr><tr>";
protected const string HTML4 = "</tr></table>";
private static readonly Regex RX = new Regex(@"^&page=d+", RegexOptions.Compiled);
private const string LINK_PREV = "<td><a href=?page={0}> 上一頁</a></td>";
private const string LINK_MORE = "<td><a href=?page={0}>下一頁 </a></td>";
private const string LINK_DISPLAY = "<td><a href=?page={0}> {1} </a></td>";
private const string KEY_PAGE = "page";
private const string COMMA = "?";
private const string AMP = "&";
protected string emptyText;
private IList dataSource;
private int pageSize = 10;
private int currentPageIndex;
private int itemCount;
override public object DataSource ...{
set ...{
//This try catch block is to avoid issues with the VS.NET designer
//The designer will try and bind a datasource which does not derive from ILIST
try ...{
dataSource = (IList)value;
ItemCount = dataSource.Count;
}
catch ...{
dataSource = null;
ItemCount = 0;
}
}
}
public int PageSize ...{
get ...{ return pageSize; }
set ...{ pageSize = value; }
}
protected int PageCount ...{
get ...{ return (ItemCount - 1) / pageSize; }
}
virtual protected int ItemCount ...{
get ...{ return itemCount; }
set ...{ itemCount = value; }
}
virtual public int CurrentPageIndex ...{
get ...{ return currentPageIndex; }
set ...{ currentPageIndex = value; }
}
public string EmptyText ...{
set ...{ emptyText = value; }
}
public void SetPage(int index) ...{
OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
}
override protected void OnLoad(EventArgs e) ...{
if (Visible) ...{
string page = Context.Request[KEY_PAGE];
int index = (page != null) ? int.Parse(page)-1 : 0;
SetPage(index);
}
}
/** <summary>
/// Overriden method to control how the page is rendered
/// </summary>
/// <param name="writer"></param>
override protected void Render(HtmlTextWriter writer) ...{
//Check there is some data attached
if (ItemCount == 0) ...{
writer.Write(emptyText);
return;
}
//Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);
// Write out the first part of the control, the table header
writer.Write(HTML1);
// Call the inherited method
base.Render(writer);
// Write out a table row closure
writer.Write(HTML2);
//清單資訊
writer.Write("<td>共 " + PageCount + " 頁 " + itemCount + " 項</td>");
//導航到上一頁
if (currentPageIndex > 0)
writer.Write(string.Format(LINK_PREV, (currentPageIndex ) + query));
else
...{
writer.Write("<td> 上一頁 </td>");
}
//得到頁碼導航
for (int i =0; i <= 9 && i <= PageCount && (currentPageIndex - currentPageIndex % 10 + i)<=PageCount ; i++)
...{
if (currentPageIndex - currentPageIndex % 10 + i == currentPageIndex)
...{
writer.Write("<td> " + (currentPageIndex+1) + " </td>");
}
else
...{
writer.Write(string.Format(LINK_DISPLAY, (currentPageIndex - currentPageIndex % 10 + i+1) + query,(currentPageIndex - currentPageIndex % 10 + i+1) ));
}
}
//導航到下一頁
if (currentPageIndex < PageCount)
writer.Write(string.Format(LINK_MORE, (currentPageIndex + 2) + query));
else
...{
writer.Write("<td> 下一頁 </td>");
}
//Close the table
writer.Write(HTML4);
}
override protected void OnDataBinding(EventArgs e) ...{
//Work out which items we want to render to the page
int start = CurrentPageIndex * pageSize;
int size = Math.Min(pageSize, ItemCount - start);
IList page = new ArrayList();
//Add the relevant items from the datasource
for (int i = 0; i < size; i++)
page.Add(dataSource[start + i]);
//set the base objects datasource
base.DataSource = page;
base.OnDataBinding(e);
}
public event DataGridPageChangedEventHandler PageIndexChanged;
virtual protected void OnPageIndexChanged(DataGridPageChangedEventArgs e) ...{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
}
}
三、建立一個類庫工程,把上面代碼儲存為CustomList.cs,再重新生成,在工具欄便會多出一個使用者自定義元件。
四、對其進行調用
1.建立網站,在新頁面中拖拽一個CustomList元件。
2.對CustomList元件進行前台屬性設定
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%...@ Register Assembly="CustomList" Namespace="CustomList" TagPrefix="cc1" %>
<!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">
<cc1:CustomList ID="productsList" runat="server" EmptyText="No products found." OnPageIndexChanged="PageChanged" PageSize="12" RepeatColumns="1" CellPadding="4" Width="400px" CurrentPageIndex="1" ForeColor="#333333">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top" width="400px" align="center" ><%...# Eval("dc")%></td>
</tr>
</table>
</ItemTemplate>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</cc1:CustomList>
</form>
</body>
</html>
3.背景頁面程式綁定
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
...{
//reset index
productsList.CurrentPageIndex = e.NewPageIndex;
//構造要作為資料源顯示的表
DataTable dt = new DataTable("tabe1");
DataColumn dc = new DataColumn("dc");
dt.Columns.Add(dc);//構造一列
for (int i = 1; i <= 100; i++)//循環加入資料
...{
DataRow dr = dt.NewRow();
dr["dc"] ="資料"+ i;
dt.Rows.Add(dr);
}
//bind data
productsList.DataSource = dt.DefaultView;
productsList.DataBind();
}
}
本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/chenmintong/archive/2007/06/27/1669427.aspx