2.0下对datalist控件分页操作控件制作

事先说明,原理做的很简单,取出一数据集,对其进行操作.
控件使用方法:
首先把下面的代码copy进你的新项目,编译成dll后,然后加载进你的工程.
从工具箱中拉出datalist,和本控件"DataListControl"到页面.
代码实现
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.ComponentModel;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  DataListPageControl.WjbsControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif    
public class DataListControl: CompositeControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private Label lbNowPageNumber = new Label();
InBlock.gif        
private Button btPageUp = new Button();
InBlock.gif        
private Button btPageDown = new Button();
InBlock.gif        
private Button btPageFirst = new Button();
InBlock.gif        
private Button btPageLast = new Button();
InBlock.gif        
private string ddlName;
InBlock.gif        
private int m_PageNow = 1;                    //当前页号
InBlock.gif
        private DataSet m_ds = new DataSet();        //传入的数据集合
InBlock.gif
        private int m_PageSize = 5;                    //页面布局;
InBlock.gif

InBlock.gif        
protected override object SaveViewState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] o = new object[5];
InBlock.gif            o[
0= ddlName;
InBlock.gif            o[
1= m_PageNow;
InBlock.gif            o[
2= m_ds;
InBlock.gif            o[
3= m_PageSize;
InBlock.gif        
InBlock.gif            
return o;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void LoadViewState(object savedState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object[] o = new object[4];
InBlock.gif            o 
= (object[])savedState;
InBlock.gif            ddlName 
= (string)o[0];
InBlock.gif            DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif            m_PageNow 
= int.Parse( o[1].ToString());
InBlock.gif            m_ds 
= (DataSet)o[2];
InBlock.gif            m_PageSize 
= int.Parse(o[3].ToString());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lbNowPageNumber.Text 
= "1/1";
InBlock.gif            btPageUp.Text 
= "上一面";            
InBlock.gif            btPageDown.Text 
= "下一面";            
InBlock.gif            btPageFirst.Text 
= "第一面";            
InBlock.gif            btPageLast.Text 
= ">>>>";
InBlock.gif
InBlock.gif            btPageUp.Click 
+= new EventHandler(btPageUp_Click);
InBlock.gif            btPageDown.Click
+=new EventHandler(btPageDown_Click);
InBlock.gif            btPageFirst.Click
+=new EventHandler(btPageFirst_Click);     
InBlock.gif            btPageLast.Click
+=new EventHandler(btPageLast_Click);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btPageDown_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( null == m_ds || 0 == m_ds.Tables[0].Rows.Count)
InBlock.gif                
throw new Exception("请给出数据源");
InBlock.gif            
if (null == ddlName)
InBlock.gif                
throw new Exception("请指定datalist控件");
InBlock.gif
InBlock.gif            
if (m_PageNow < PageCout())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif                ddl.DataSource 
= PageNext();
InBlock.gif                ddl.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btPageFirst_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (null == m_ds || 0 == m_ds.Tables[0].Rows.Count)
InBlock.gif                
throw new Exception("请给出数据源");
InBlock.gif            
if (m_PageNow == 1return;
InBlock.gif            DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif            ddl.DataSource 
= pageFirst();
InBlock.gif            ddl.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btPageLast_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (null == m_ds || 0 == m_ds.Tables[0].Rows.Count)
InBlock.gif                
throw new Exception("请给出数据源");
InBlock.gif            
if (m_PageNow < PageCout())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif                ddl.DataSource 
= pageLast();
InBlock.gif                ddl.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btPageUp_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (null == m_ds || 0 == m_ds.Tables[0].Rows.Count)
InBlock.gif                
throw new Exception("请给出数据源");
InBlock.gif            
if (m_PageNow == 1return;
InBlock.gif            DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif            ddl.DataSource 
= PageUp();
InBlock.gif            ddl.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Controls.Add(lbNowPageNumber);
InBlock.gif            Controls.Add(
new LiteralControl("   ") );
InBlock.gif            Controls.Add(btPageUp);
InBlock.gif            Controls.Add(
new LiteralControl("   ") );
InBlock.gif            Controls.Add(btPageDown);
InBlock.gif            Controls.Add(
new LiteralControl("   "));
InBlock.gif            Controls.Add(btPageFirst);
InBlock.gif            Controls.Add(
new LiteralControl("   "));
InBlock.gif            Controls.Add(btPageLast);
InBlock.gif
InBlock.gif           
InBlock.gif                
if (null == m_ds)
InBlock.gif                    
throw new Exception("请给出数据源");
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataList ddl 
= ((DataList)this.Page.FindControl(ddlName));
InBlock.gif                ddl.DataSource 
= GotePageN(m_PageNow);
InBlock.gif                ddl.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 控件名称
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Browsable(false), Description("绑定的datalist控件名"), DefaultValue("DataList1")]
InBlock.gif        
public string ControlName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ddlName 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 无参构造
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public DataListControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 有参构造
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="ds">传入的数据集</param>

InBlock.gif        public DataListControl(DataSet ds)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            DataSetSourse 
= ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 传入的数据集
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Browsable(false),Description("数据集形式的数据源")]
InBlock.gif        
public DataSet DataSetSourse
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_ds = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 页面布局
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [Browsable(true),Description("每页显示项数")]
InBlock.gif        
public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_PageSize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_PageSize = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 打开一页
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pageNo">当前页</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回操作的结果</returns>

InBlock.gif        public DataSet GotePageN(int m_PageNow)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsA 
= new DataSet();
InBlock.gif            dsA 
= m_ds.Clone();
InBlock.gif            
int a = PageCout();//获取该记录集的总条数
InBlock.gif
            if (m_PageNow <= a && m_PageNow >= 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rowNum = (m_PageNow - 1* PageSize;
InBlock.gif                
for (int i = rowNum; i < rowNum + PageSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i >= m_ds.Tables[0].Rows.Count)
InBlock.gif                        
break;
InBlock.gif                    
if (m_ds.Tables[0].Rows[i] != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        dsA.Tables[
0].ImportRow((m_ds.Tables[0].Rows[i]) as DataRow);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            lbNowPageNumber.Text 
= m_PageNow.ToString() + "/" + PageCout().ToString();
InBlock.gif
InBlock.gif            
return dsA;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回总页数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public int PageCout()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int rowCount = m_ds.Tables[0].Rows.Count;
InBlock.gif            
int PageTemp = (int)(rowCount / m_PageSize);
InBlock.gif            
if (rowCount % m_PageSize == 0)
InBlock.gif                
return PageTemp;
InBlock.gif            
else
InBlock.gif                
return (PageTemp + 1);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 下一页
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pageNo">当前页面</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回结果</returns>

InBlock.gif        public DataSet PageNext()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsA 
= new DataSet();
InBlock.gif            dsA 
= m_ds.Clone();
InBlock.gif            
int a = PageCout();//获取该记录集的总条数
InBlock.gif
            if (m_PageNow < a)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rowNum = m_PageNow * PageSize;
InBlock.gif                
for (int i = rowNum; i < rowNum + PageSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i >= m_ds.Tables[0].Rows.Count)
InBlock.gif                        
break;
InBlock.gif                    dsA.Tables[
0].ImportRow((m_ds.Tables[0].Rows[i]) as DataRow);
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_PageNow 
+= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            lbNowPageNumber.Text 
= m_PageNow.ToString() + "/" + PageCout().ToString();
InBlock.gif            
InBlock.gif            
return dsA;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 上一页
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="pageNo">当前页面</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataSet PageUp()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsA 
= new DataSet();
InBlock.gif            dsA 
= m_ds.Clone();
InBlock.gif            
int a = PageCout();//获取该记录集的总条数
InBlock.gif
            if (m_PageNow >= 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rowNum = (m_PageNow - 2* PageSize;
InBlock.gif                
for (int i = rowNum; i < rowNum + PageSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i >= m_ds.Tables[0].Rows.Count)
InBlock.gif                        
break;
InBlock.gif                    dsA.Tables[
0].ImportRow((m_ds.Tables[0].Rows[i]) as DataRow);
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_PageNow 
-= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            lbNowPageNumber.Text 
= m_PageNow.ToString() + "/" + PageCout().ToString();
InBlock.gif            
InBlock.gif            
return dsA;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 转到首页
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>返回查询结果</returns>

InBlock.gif        public DataSet pageFirst()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsA 
= new DataSet();
InBlock.gif            dsA 
= m_ds.Clone();
InBlock.gif            
int a = PageCout();//获取该记录集的总条数
InBlock.gif
            if (a != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rowNum = 0;
InBlock.gif                
for (int i = rowNum; i < rowNum + PageSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i >= m_ds.Tables[0].Rows.Count)
InBlock.gif                        
break;
InBlock.gif                    dsA.Tables[
0].ImportRow((m_ds.Tables[0].Rows[i]) as DataRow);
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_PageNow 
= 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            lbNowPageNumber.Text 
= m_PageNow.ToString() + "/" + PageCout().ToString();
InBlock.gif            
InBlock.gif            
return dsA;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 最后一页
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public DataSet pageLast()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsA 
= new DataSet();
InBlock.gif            dsA 
= m_ds.Clone();
InBlock.gif            
int a = PageCout();//获取该记录集的总条数
InBlock.gif
            if (a != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int rowNum = (a - 1* PageSize;
InBlock.gif                
for (int i = rowNum; i < rowNum + PageSize; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (i >= m_ds.Tables[0].Rows.Count)
InBlock.gif                        
break;
InBlock.gif                    dsA.Tables[
0].ImportRow((m_ds.Tables[0].Rows[i]) as DataRow);
ExpandedSubBlockEnd.gif                }

InBlock.gif                m_PageNow 
= a;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            lbNowPageNumber.Text 
= m_PageNow.ToString() + "/" + PageCout().ToString();
InBlock.gif            
InBlock.gif            
return dsA;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/wjbs7188/archive/2007/04/04/700230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值