qhnokia

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

第一步:新建控件库

代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace CustomServerControlsLib
{
public class CustomGrid : Repeater
{
//Static constants
protected const string HTML1 = "<table cellpadding=0 cellspacing=0><tr><td colspan=2>";
protected const string HTML2 = "</td></tr><tr><td class=paging align=left>";
protected const string HTML3 = "</td><td align=right class=paging>";
protected const string HTML4 = "</td></tr></table>";
private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
private const string LINK_PREV = "<a href=?page={0}>&#060;&nbsp;上一页</a>";
private const string LINK_MORE = "<a href=?page={0}>下一页&nbsp;&#062;</a>";
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) : 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);

//Determin whether next and previous buttons are required
//Previous button?
if (currentPageIndex > 0)
writer.Write(
string.Format(LINK_PREV, (currentPageIndex - 1) + query));

//Close the table data tag
writer.Write(HTML3);

//Next button?
if (currentPageIndex < PageCount)
writer.Write(
string.Format(LINK_MORE, (currentPageIndex + 1) + query));

//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);
}
}
}

 

第二步:新建用户控件

 

前台
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CarGridControl.ascx.cs" Inherits="分页实践.CarGridControl" %>
<%@ Register TagPrefix="CC" Namespace="CustomServerControlsLib" Assembly="CustomServerControlsLib" %>
<CC:CustomGrid ID="CarGrid" runat="server" EmptyText="暂时没有记录" PageSize="10" EnableViewState="false"
OnPageIndexChanged
="PageChanged" onload="CarGrid_Load">
<HeaderTemplate>
<table cellspacing="0" cellpadding="0" border="0" width="387">
</HeaderTemplate>
<ItemTemplate>
<tr><td><table> <tr align="left" valign="top">
<td width="100"><%# Eval("logo") %></td>
<td width="100"><%# Eval("price", "{0:c}") %></td>
</tr> </table></td></tr>


</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</CC:CustomGrid>

 

后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 分页实践.Model;
using System.Data.SqlClient;

namespace 分页实践
{
public partial class CarGridControl : System.Web.UI.UserControl
{
public void PageChanged(object sender, DataGridPageChangedEventArgs e)
{
CarGrid.CurrentPageIndex = e.NewPageIndex;
IList
<ItemInfo> carlist = new List<ItemInfo>();

using( SqlDataReader dr = SqlHelper.ExecuteReader(
SqlHelper.ConnectionStringLocalTransaction
, System.Data.CommandType.Text
, "select logo, price from car"
, null ) )
{


while (dr.Read()){
ItemInfo item = new ItemInfo(dr.GetString(0), dr.GetDecimal(1));
carlist.Add(item);
}
}
CarGrid.DataSource = carlist;
CarGrid.DataBind();

}

protected void CarGrid_Load(object sender, EventArgs e)
{



}

}
}

 

一个Model

 

代码
using System;

namespace 分页实践.Model
{
///
<summary>
/// Business entity used to model an item.
///
</summary>
[Serializable]
public class ItemInfo
{

// Internal member variables
private string logo;
private decimal price;

public ItemInfo() { }
public ItemInfo(string logo, decimal price)
{
this.logo = logo;
this.price = price;
}

// Properties

public string Logo
{
get { return logo; }
}



public decimal Price
{
get { return price; }
}




}
}

 

第三步:表示层使用用户控件

 

表示层使用用户控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="分页实践.WebForm1" %>
<%@ Register src="~/Controls/CarGridControl.ascx" tagname="CarGridControl" tagprefix="uc1" %>
<!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">
<div>


<uc1:CarGridControl ID="CarGridControl1" runat="server" />


</div>
</form>
</body>
</html>

 

posted on 2010-01-10 21:02  其乐无穷  阅读(303)  评论(0编辑  收藏  举报