一、概述
在web開發(fā)中,常常需要顯示一些數(shù)據(jù),而為了方便排版及瀏覽,我們只需要顯示所有記錄中的一部分。一般情況下,我們采用分頁(yè)來(lái)實(shí)現(xiàn)這個(gè)需求。實(shí)現(xiàn)分頁(yè)的方法多種多樣,在本文中,我們采用了一個(gè)分頁(yè)空間來(lái)記錄記錄總數(shù)、當(dāng)前頁(yè)、總頁(yè)數(shù)及頁(yè)面大小等。為了有一個(gè)直觀上的印象,先展示該控件運(yùn)行后的效果,效果如下圖所示:
二、實(shí)現(xiàn)方案
為了實(shí)現(xiàn)該效果圖,在asp.net中,可以使用Custom Controls and User Controls兩種方式,User Controls的實(shí)現(xiàn)方式及其簡(jiǎn)單,而且使用起來(lái)和平時(shí)使用Controls的方式差別極大,所以我們采用Custom Controls實(shí)現(xiàn)。
參考資料:Professional ASP.NET 2.0 Server Control and Component Development
三、分頁(yè)控件的實(shí)現(xiàn)
1)、新建一個(gè)ASP.NET Server Control項(xiàng)目,
2)、在該項(xiàng)目中添加一個(gè)ASP.NET Server Control的Item,并設(shè)置其Name為PageOn,
3)、修改該類繼承于CompositeControl類,并修改其Attribute為如下所示:
復(fù)制代碼 代碼如下:
[DefaultProperty("PageSize")]
[ToolboxData("{0}:PageOn runat=server Width=100%>/{0}:PageOn>")]
public class PageOn : CompositeControl
注:自定義控件必須繼承自Control或者其子類。
4)、 定義需要被組合的控件
復(fù)制代碼 代碼如下:
Label lblMessage;
LinkButton btnFirst;
LinkButton btnPrev;
LinkButton btnNext;
LinkButton btnLast;
TextBox txtGoPage;
Button btnGo;
5)、定義分頁(yè)控件需要用到的Proptery
分頁(yè)控件主要包括頁(yè)面大小、當(dāng)前頁(yè)、總記錄數(shù)及總頁(yè)數(shù)等屬性,并需要保存在ViewState中,詳細(xì)代碼如下所示:
復(fù)制代碼 代碼如下:
public int RowCount
{
get
{
if (ViewState["m_rowCount"] == null || int.Parse(ViewState["m_rowCount"].ToString()) 0)
{
ViewState["m_rowCount"] = 0;
}
return int.Parse(ViewState["m_rowCount"].ToString());
}
set
{
if (value 0)
{
ViewState["m_rowCount"] = 0;
}
else
{
ViewState["m_rowCount"] = value;
}
this.RecreateChildControls();
}
}
public int CurPage
{
get
{
if (ViewState["m_curPage"] ==null || int.Parse(ViewState["m_curPage"].ToString()) 1)
{
ViewState["m_curPage"] = 1;
}
return int.Parse(ViewState["m_curPage"].ToString());
}
set
{
if (value 1)
{
ViewState["m_curPage"] = 1;
}
else if (value > PageCount)
{
ViewState["m_curPage"] = PageCount;
}
else
{
ViewState["m_curPage"] = value;
}
}
}
public int PageCount
{
get
{
return RowCount / PageSize + 1;
}
}
public int PageSize
{
get
{
if (ViewState["m_pageSize"] ==null || int.Parse(ViewState["m_pageSize"].ToString()) 1)
{
ViewState["m_pageSize"] = 15;
}
return int.Parse(ViewState["m_pageSize"].ToString());
}
set
{
if (value > 0)
{
ViewState["m_pageSize"] = value;
this.RecreateChildControls();
}
}
}
6)、生成自定義控件的子空間
生成自定義空間的子空間需要override基類Control中的CreateChildControls()方法,詳細(xì)代碼如下所示:
復(fù)制代碼 代碼如下:
protected override void CreateChildControls()
{
Controls.Clear();
lblMessage = new Label();
lblMessage.Text = "當(dāng)前第" + CurPage + "頁(yè)nbsp;共" + PageCount + "頁(yè) nbsp;共" + RowCount + "條記錄";
lblMessage.ID = "lblMessage";
Controls.Add(lblMessage);
btnFirst = new LinkButton();
btnFirst.Text = "首頁(yè)";
btnFirst.CommandName = "first";
btnFirst.ID = "btnFirst";
if (CurPage = 1)
{
btnFirst.Enabled = false;
}
Controls.Add(btnFirst);
btnPrev = new LinkButton();
btnPrev.Text = "上一頁(yè)";
btnPrev.CommandName = "prev";
btnPrev.ID = "btnPrev";
if (CurPage = 1)
{
btnPrev.Enabled = false;
}
Controls.Add(btnPrev);
btnNext = new LinkButton();
btnNext.Text = "下一頁(yè)";
btnNext.CommandName = "next";
btnNext.ID = "btnNext";
if (CurPage >= PageCount)
{
btnNext.Enabled = false;
}
Controls.Add(btnNext);
btnLast = new LinkButton();
btnLast.Text = "末頁(yè)";
btnLast.CommandName = "last";
btnLast.ID = "btnLast";
if (CurPage >= PageCount)
{
btnLast.Enabled = false;
}
Controls.Add(btnLast);
txtGoPage = new TextBox();
txtGoPage.TabIndex = 1;
txtGoPage.ID = "txtGoPage";
txtGoPage.Attributes.Add("onkeyup", @"this.value=this.value.replace(/\D/g,'')");
txtGoPage.Attributes.Add("onafterpaste", @"this.value=this.value.replace(/\D/g,'')");
Controls.Add(txtGoPage);
btnGo = new Button();
btnGo.TabIndex = 2;
btnGo.CommandName = "go";
btnGo.Text = "GO";
btnGo.ID="btnGO";
Controls.Add(btnGo);
Debug.WriteLine("ffffffffffffffffffffffffffffffffffffffffffffffffff");
base.CreateChildControls();
}
7)、定義自定義控件的布局
第6步完成后,所有定義的控件都會(huì)順序顯示到頁(yè)面上了,但是這樣的效果不友好,如果對(duì)于多行的空間更是如此,所有我們需要定義控件的布局,自定義控件的布局需要重寫RenderContents()方法及TagKey屬性,此示例中的代碼如下所示:
復(fù)制代碼 代碼如下:
protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.AddStyleAttribute("text-align", "left");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write("nbsp;nbsp;");
lblMessage.RenderControl(output);
output.RenderEndTag();
output.AddStyleAttribute("text-align", "right");
output.RenderBeginTag(HtmlTextWriterTag.Td);
btnFirst.RenderControl(output);
output.Write("nbsp;nbsp;");
btnPrev.RenderControl(output);
output.Write("nbsp;nbsp;");
btnNext.RenderControl(output);
output.Write("nbsp;nbsp;");
btnLast.RenderControl(output);
output.Write("到");
output.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
txtGoPage.RenderControl(output);
output.Write("頁(yè)");
btnGo.RenderControl(output);
output.Write("nbsp;nbsp;");
output.RenderEndTag();
output.RenderEndTag();
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Table;
}
}
上面的代碼中,我們使用Table來(lái)布局,也可以使用其它的布局方式,比如DIV+CSS。
8)、捕捉并處理控件的事件
到此以后,這些代碼已經(jīng)可以生成文章開頭圖所顯示的效果,但是什么事情也做不了,不能響應(yīng)該控件上的事件,所有還需要繼續(xù)實(shí)現(xiàn)該控件上的事件代碼,實(shí)現(xiàn)這些事件采用冒泡所有子控件的事件來(lái)實(shí)現(xiàn)。
首先,定義一個(gè)委托:
復(fù)制代碼 代碼如下:
public delegate void PageOnEventHandler(object sender, EventArgs args);
其次,定義基于該委托的事件:
復(fù)制代碼 代碼如下:
public event PageOnEventHandler RecPageChanged;
最后,重寫冒泡事件,并根據(jù)參數(shù)特征,捕獲需要處理的事件,使其調(diào)用需要的方法。
復(fù)制代碼 代碼如下:
protected override bool OnBubbleEvent(object source, EventArgs args)
{
bool handled = false;
CommandEventArgs cea = args as CommandEventArgs;
if(cea == null)
{
return handled;
}
switch (cea.CommandName)
{
case "first":
handled = true;
CurPage = 1;
break;
case "prev":
handled = true;
if (CurPage > 1)
{
CurPage--;
}
else
{
CurPage = 1;
}
break;
case "next":
handled = true;
if (CurPage PageCount)
{
CurPage ++ ;
}
else
{
CurPage = PageCount;
}
break;
case "last":
handled = true;
CurPage = PageCount;
break;
case "go":
string strGo = txtGoPage.Text.Trim();
int iGo;
if (!string.IsNullOrEmpty(strGo) int.TryParse(strGo, out iGo))
{
handled = true;
CurPage = iGo;
}
break;
}
if (handled)
{
if (this.RecPageChanged != null)
{
RecPageChanged(this, args);
this.RecreateChildControls();
}
return handled;
}
else
{
return base.OnBubbleEvent(source, args);
}
}
到此就完成了分頁(yè)控件的開發(fā)。
注:可以定制該控件的樣式,或者使用屬性暴露子控件的屬性來(lái)控制樣式等.
四、使用分頁(yè)控件
完成自定義控件的開發(fā)后,在Toolbox中Choose Items或者直接在需要使用該自定義控件的項(xiàng)目中引用該項(xiàng)目或者DLL,即可在Toolbox中顯示自定義控件了。然后用拖拽的方式即可把分頁(yè)控件放到需要的地方,就像使用button控件一樣簡(jiǎn)單。
然后再該頁(yè)面的后臺(tái)代碼的OnLoad事件中,注冊(cè)需要被調(diào)用的方法到該控件的RecPageChanged事件中,如下所示:
復(fù)制代碼 代碼如下:
PageOn1.RecPageChanged += new CustomControl.PageOnEventHandler(PageOn1_RecPageChanged);
最后,只需要在方法PageOn1_RecPageChanged中編寫自己的代碼即可。
復(fù)制代碼 代碼如下:
void PageOn1_RecPageChanged(object sender, EventArgs args)
{
//To do something
}
控件的詳細(xì)代碼如下:
復(fù)制代碼 代碼如下:
namespace CustomControl
{
public delegate void PageOnEventHandler(object sender, EventArgs args);
[DefaultProperty("PageSize")]
[ToolboxData("{0}:PageOn runat=server Width=100%>/{0}:PageOn>")]
public class PageOn :CompositeControl
{
#region
Label lblMessage;
LinkButton btnFirst;
LinkButton btnPrev;
LinkButton btnNext;
LinkButton btnLast;
TextBox txtGoPage;
Button btnGo;
#endregion
protected override void CreateChildControls()
{
Controls.Clear();
lblMessage = new Label();
lblMessage.Text = "當(dāng)前第" + CurPage + "頁(yè)nbsp;共" + PageCount + "頁(yè) nbsp;共" + RowCount + "條記錄";
lblMessage.ID = "lblMessage";
Controls.Add(lblMessage);
btnFirst = new LinkButton();
btnFirst.Text = "首頁(yè)";
btnFirst.CommandName = "first";
btnFirst.ID = "btnFirst";
if (CurPage = 1)
{
btnFirst.Enabled = false;
}
Controls.Add(btnFirst);
btnPrev = new LinkButton();
btnPrev.Text = "上一頁(yè)";
btnPrev.CommandName = "prev";
btnPrev.ID = "btnPrev";
if (CurPage = 1)
{
btnPrev.Enabled = false;
}
Controls.Add(btnPrev);
btnNext = new LinkButton();
btnNext.Text = "下一頁(yè)";
btnNext.CommandName = "next";
btnNext.ID = "btnNext";
if (CurPage >= PageCount)
{
btnNext.Enabled = false;
}
Controls.Add(btnNext);
btnLast = new LinkButton();
btnLast.Text = "末頁(yè)";
btnLast.CommandName = "last";
btnLast.ID = "btnLast";
if (CurPage >= PageCount)
{
btnLast.Enabled = false;
}
Controls.Add(btnLast);
txtGoPage = new TextBox();
txtGoPage.TabIndex = 1;
txtGoPage.ID = "txtGoPage";
txtGoPage.Attributes.Add("onkeyup", @"this.value=this.value.replace(/\D/g,'')");
txtGoPage.Attributes.Add("onafterpaste", @"this.value=this.value.replace(/\D/g,'')");
Controls.Add(txtGoPage);
btnGo = new Button();
btnGo.TabIndex = 2;
btnGo.CommandName = "go";
btnGo.Text = "GO";
btnGo.ID="btnGO";
Controls.Add(btnGo);
Debug.WriteLine("ffffffffffffffffffffffffffffffffffffffffffffffffff");
base.CreateChildControls();
}
public int RowCount
{
get
{
if (ViewState["m_rowCount"] == null || int.Parse(ViewState["m_rowCount"].ToString()) 0)
{
ViewState["m_rowCount"] = 0;
}
return int.Parse(ViewState["m_rowCount"].ToString());
}
set
{
if (value 0)
{
ViewState["m_rowCount"] = 0;
}
else
{
ViewState["m_rowCount"] = value;
}
this.RecreateChildControls();
}
}
public int CurPage
{
get
{
if (ViewState["m_curPage"] ==null || int.Parse(ViewState["m_curPage"].ToString()) 1)
{
ViewState["m_curPage"] = 1;
}
return int.Parse(ViewState["m_curPage"].ToString());
}
set
{
if (value 1)
{
ViewState["m_curPage"] = 1;
}
else if (value > PageCount)
{
ViewState["m_curPage"] = PageCount;
}
else
{
ViewState["m_curPage"] = value;
}
}
}
public int PageCount
{
get
{
return RowCount / PageSize + 1;
}
}
public int PageSize
{
get
{
if (ViewState["m_pageSize"] ==null || int.Parse(ViewState["m_pageSize"].ToString()) 1)
{
ViewState["m_pageSize"] = 15;
}
return int.Parse(ViewState["m_pageSize"].ToString());
}
set
{
if (value > 0)
{
ViewState["m_pageSize"] = value;
this.RecreateChildControls();
}
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.AddStyleAttribute("text-align", "left");
output.RenderBeginTag(HtmlTextWriterTag.Td);
output.Write("nbsp;nbsp;");
lblMessage.RenderControl(output);
output.RenderEndTag();
output.AddStyleAttribute("text-align", "right");
output.RenderBeginTag(HtmlTextWriterTag.Td);
btnFirst.RenderControl(output);
output.Write("nbsp;nbsp;");
btnPrev.RenderControl(output);
output.Write("nbsp;nbsp;");
btnNext.RenderControl(output);
output.Write("nbsp;nbsp;");
btnLast.RenderControl(output);
output.Write("到");
output.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
txtGoPage.RenderControl(output);
output.Write("頁(yè)");
btnGo.RenderControl(output);
output.Write("nbsp;nbsp;");
output.RenderEndTag();
output.RenderEndTag();
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Table;
}
}
public event PageOnEventHandler RecPageChanged;
protected override bool OnBubbleEvent(object source, EventArgs args)
{
bool handled = false;
CommandEventArgs cea = args as CommandEventArgs;
if(cea == null)
{
return handled;
}
switch (cea.CommandName)
{
case "first":
handled = true;
CurPage = 1;
break;
case "prev":
handled = true;
if (CurPage > 1)
{
CurPage--;
}
else
{
CurPage = 1;
}
break;
case "next":
handled = true;
if (CurPage PageCount)
{
CurPage ++ ;
}
else
{
CurPage = PageCount;
}
break;
case "last":
handled = true;
CurPage = PageCount;
break;
case "go":
string strGo = txtGoPage.Text.Trim();
int iGo;
if (!string.IsNullOrEmpty(strGo) int.TryParse(strGo, out iGo))
{
handled = true;
CurPage = iGo;
}
break;
}
if (handled)
{
if (this.RecPageChanged != null)
{
RecPageChanged(this, args);
this.RecreateChildControls();
}
return handled;
}
else
{
return base.OnBubbleEvent(source, args);
}
}
}
}
OK,完成
您可能感興趣的文章:- asp.net webform自定義分頁(yè)控件
- asp.net分頁(yè)控件使用詳解【附實(shí)例下載】
- 解析asp.net的分頁(yè)控件
- asp.net自定義分頁(yè)控件示例
- 關(guān)于asp.net 自定義分頁(yè)控件
- 分享一個(gè)asp.net pager分頁(yè)控件
- asp.net分頁(yè)控件AspNetPager的樣式美化
- asp.net下Repeater使用 AspNetPager分頁(yè)控件
- AspNetAjaxPager,Asp.Net通用無(wú)刷新Ajax分頁(yè)控件,支持多樣式多數(shù)據(jù)綁定
- asp.net web頁(yè)面自定義分頁(yè)控件使用詳解