带搜索的分页控件之一
/********************************************************************
* FileName : pagefoot.cs
* Target : 翻页连接控件,可提供对搜索的支持
* Author : Baihao
* CreateDate : 2003/03/04 15:50
* LastModify : 2003/03/04 15:50
* History :
2003/03/04 15:50 创建
*********************************************************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Collections;
namespace EII.Lib.WebControls
{
///
/// 搜索事件处理
///
public delegate void SearchHandle(string sInput,int nIndex);
///
/// 翻页连接控件,可提供对搜索的支持
///
[Designer("EII.Lib.WebControls.Design.PageFootDesign"), DefaultProperty("CurrentPage"), ToolboxData("<{0}:PageFoot runat=server>"), System.ComponentModel.ToolboxItem(true)]
public class PageFoot : System.Web.UI.UserControl
{
#region Const Define
private const string GOTO = "PF_txtGoto";
private const string DROP = "PF_dplSearch";
private const string END = "";
private const string HIDDEN = "PF_Hidden";
#endregion
#region Member variable
private string current = "";
///
/// 在点击了搜索后,激发的事件,使用时要处理
///
public event SearchHandle SearchEvent ;
protected string m_sIndex = "?PageIndex=";
///
/// 当前页数
///
protected int nPage = 1;
///
/// 每页条数
///
protected int nPageSize = 10;
///
/// 总记录数
///
protected int nRowCount = 1;
///
/// 总页数
///
private int nPageCount = 1;
// ///
// /// 总页数, 只读
// ///
// public int PageCount
// {
// get{return this.nPageCount ;}
// }
///
/// 第一页的连接
///
protected string FirstHead = "";
///
/// 前一页的连接
///
protected string PreHead = "";
///
/// 下一页的连接
///
protected string NextHead = "";
///
/// 最后一页的连接
///
protected string LastHead = "";
///
/// 关闭第一页的连接
///
protected string FirstFoot = "";
///
/// 关闭前一页的连接
///
protected string PreFoot = "";
///
/// 关闭后一页的连接
///
protected string NextFoot = "";
///
/// 关闭最后一页的连接
///
protected string LastFoot = "";
///
/// 是否显示搜索栏
///
public bool ShowSearch = false;
// public string ControlName = "";
private ArrayList al;
private ImageButton btnFoot_Search;
private ImageButton btnFoot_GO;
private System.Web.UI.WebControls.Label Label1;
private TextBox stxt;
private System.Web.UI.WebControls.TextBox txtFoot_Goto;
private System.Web.UI.WebControls.DropDownList SearchTarget;
private bool bInit = false;
#endregion
#region Property
///
/// 页面的参数,用于在分页时读取页数(eg:PageIndex=14)
///
public string PageIndex
{
set
{
m_sIndex = "?"+value+"=";
if(current.IndexOf('?') >= 0)
m_sIndex = m_sIndex.Replace('?','&');
}
get{return (m_sIndex.Length>0)?m_sIndex.Substring(1,m_sIndex.Length-2):"";}
}
// public event EventHandler Click;
///
/// 当前页面的名称,用于在分页时形成指向另一个页面的连接(eg:List.aspx)
///
public string CurrentPage
{
get
{
return current;
}
set
{
current = value;
}
}
#endregion
#region Public Method
///
/// 改变搜索控件中的选择项目:用字符串数组
///
///
public void ChangeSelectItem(string[] ss)
{
al = new ArrayList();
foreach(string s in ss)
{
al.Add(s);
}
}
///
/// 改变搜索控件中的选择项目:用ArrayList
///
///
public void ChangeSelectItem(ArrayList ss)
{
al =ss;
}
#endregion
#region Private Method
///
/// 改变连接目标
///
private void ChangeLink()
{
char cLink = '?';
if(current.IndexOf(cLink) >= 0 )
{
m_sIndex = m_sIndex.Replace(cLink,'&');
cLink = '&';
}
int i;
if( (i = current.IndexOf(m_sIndex.Substring(1))) >= 0)
{
char ch = current[i - 1];
int k = current.IndexOf(cLink,i);
if( k >= 0)
current = current.Remove(i-1 ,k + 1 -i);
else
current = current.Remove(i - 1 ,current.Length + 1 - i );
m_sIndex = m_sIndex.Replace (cLink,ch);
}
}
///
/// 添加搜索控件
///
private void AddSearch()
{
this.btnFoot_Search = new ImageButton ();
this.Label1 = new Label();
this.stxt = new TextBox();
this.SearchTarget = new DropDownList();
// this.lcnSecond = new Label();
// this.lcnFirst = new Label();
//搜索标签
Label1.Text = "搜索";
this.Controls.Add(Label1);
//搜索目标输入
stxt.MaxLength = 40;
stxt.Columns = 12;
this.Controls.Add(stxt);
//搜索条件
SearchTarget.ID = DROP;
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
int i = 0;
foreach(object o in al)
{
dr = dt.NewRow();
dr[0] = i++;
dr[1] = (string)o;
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
SearchTarget.DataSource = dv;
SearchTarget.DataTextField = "StringValue";
SearchTarget.DataValueField = "IntegerValue";
SearchTarget.DataBind();
this.Controls.Add(SearchTarget);
//搜索事件处理
btnFoot_Search.Click += new ImageClickEventHandler(Search_Click);
btnFoot_Search.ImageUrl = @"/Images/search_button.gif";
this.Controls.Add(btnFoot_Search);
}
///
/// 初始化控件
///
private void CustomInit()
{
if((current!="")&&(m_sIndex!= "")&&(nPageCount!= 0))
{
ChangeLink();
if(nPage != 1)
{
FirstHead = "";
FirstFoot = END;
}
if(nPage > 1)
{
PreHead = "";
PreFoot = END;
}
if(nPage < nPageCount)
{
NextHead = "";
NextFoot = END;
}
if(nPage != nPageCount)
{
LastHead = "";
LastFoot = END;
}
}
// create Controls
}
#endregion
///
/// 通过当前数据源显示分页控件
///
/// 当前页序
/// 每页大小
/// 数据源记录总数
///
public int Show(int page,int pagesize,int rowCount)
{
// nPage = page;
// nPageCount = countpage;
// 在此处放置用户代码以初始化页面
//Response.Write(FirstHead);
if(page <1)
page = 1;
nPage = page;
nPageSize = pagesize;
nRowCount = rowCount;
nPageCount = (nRowCount/nPageSize )+(((nRowCount%nPageSize ) == 0)?0:1);
if(nPageCount < 1)
nPageCount = 1;
if(nPage > nPageCount)
nPage = nPageCount;
DataBind();
// this.HasChildViewState = true;
return nPage;
}
///
/// 重载初始化
///
///
protected override void OnInit(EventArgs e)
{
//this.EnableViewState = true;
current = System.IO.Path.GetFileName(Request.Url.LocalPath)+Request.Url.Query ;
al = new ArrayList();
}
public override Control FindControl(string id)
{
return base.FindControl (id);
}
///
/// 重载构建子函数
///
protected override void CreateChildControls()
{
if(Request != null)
{
string id = string.Concat(this.ID,':'/*,this.ID,'_'*/,HIDDEN);
string hidden = Request[id];
if(hidden == null || hidden == "")
return;
string[] s = hidden.Split(',');
if(s== null || s.Length != 3 )
return;
try
{
nPage = int.Parse(s[0]);
nPageCount = int.Parse(s[1]);
nRowCount = int.Parse(s[2]);
DataBind();
}
catch
{
}
}
DataBind();
// bInit = true;
}
///
/// 显示控件
///
public override void DataBind()
{
if(bInit)
return;
bInit = true;
CustomInit();
if(ShowSearch)
AddSearch();
this.btnFoot_GO = new ImageButton();
this.txtFoot_Goto = new TextBox();
//页导航
Label lcnFirst= new Label();
lcnFirst.Text = "
["+FirstHead+"首页"+FirstFoot+"] ["+PreHead+"前页"+PreFoot+"] ["+NextHead+"后页"+NextFoot+"] ["+LastHead+"尾页"+LastFoot+"] 第";
this.Controls.Add(lcnFirst);
//目标页
txtFoot_Goto.Columns = 1;
txtFoot_Goto.MaxLength = 4;
txtFoot_Goto.Text = nPage.ToString();
this.Controls.Add(txtFoot_Goto);
//总页数
Label lcnSecond = new Label();
lcnSecond.Text = "页/共"+nPageCount.ToString()+@"页("+nPageSize +@"条/页,共"+nRowCount+"条)";
this.Controls.Add(lcnSecond);
//go按钮
btnFoot_GO.ImageUrl = @"/Images/go.gif";
btnFoot_GO.AlternateText= "转到";
btnFoot_GO.Click += new ImageClickEventHandler(GO_Click);
this.Controls.Add(btnFoot_GO);
// Hidden value
HtmlInputHidden hidden = new HtmlInputHidden();
hidden.ID = /*this.ID+'_'+*/HIDDEN;
hidden.Value = string.Concat(nPage,',',nPageCount,',',nRowCount);
this.Controls.Add(hidden);
//
this.Controls.Add(new LiteralControl(@""));
}
///
/// 将搜索事件传递到容器中
///
///
///
private void Search_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(SearchEvent != null)
SearchEvent(stxt.Text,SearchTarget.SelectedIndex);
}
///
/// 处理GO事件,直接重定向到目标页
///
///
///
private void GO_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//是否
if((current!="")&&(m_sIndex!= "")&&(nPageCount!= 0))
{
ChangeLink();
Response.Redirect(CurrentPage+m_sIndex+txtFoot_Goto.Text/*Request.Form[ControlName+":txtFoot_Goto"]*/ );
}
}
}
}