我的分页控件设计
数据分页在数据查询应用中是非常常用的,相信大部开发人员都使用过别人的或自己开发的。以下介绍一下本人自己开发的分页控件的设计思路。既然控件是负责分页那自然就有一个信息描述结构,控件通过信息结构进行相关内容输出。
首先制定一个结构。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public interface IDataPage
{
int PageIndex
{
get;
set;
}
int PageSize
{
get;
set;
}
int RecordCount
{
get;
set;
}
int PageCount
{
get;
}
string OrderField
{
get;
set;
}
}
以上接口提供了PageIndex, PageSize和RecordCount这三个属性基本满足了一个分页控件表现需要的数据信息。有了描述信息那就开始控件表现的描述,因为近些年来一直都采用MVC的方式开发,所以控件直接就是HTML混合C#代码输出了。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Control Language="C#" AutoEventWireup="true" %>
<%
IDataPage mDataPage = null;
int spitindex = 0;
mDataPage = MVCContext.Current.GetData<IDataPage>("datapage");
if (mDataPage == null)
mDataPage = new DataPage();
IUrlParams urlparam = MVCContext.Current.GetData<IUrlParams>("urlparams");
%>
<table id="pagebard" >
<tr>
<%
if(mDataPage.PageIndex>0)
{
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,mDataPage.PageIndex-1,urlparam) %>" >
<<</a>
</td>
<% }
spitindex =mDataPage.PageIndex - 2;
if (spitindex > 4)
{
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,0,urlparam) %>" >1</a>
</td>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,spitindex-2,urlparam) %>" >
![](https://www.cnblogs.com/Images/dot.gif)
</td>
<% }
else
{
for (int i = 0; i < spitindex; i++)
{
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,i,urlparam) %>" ><%=i + 1%></a>
</td>
<% }
}
for(int i = mDataPage.PageIndex - 2; i < mDataPage.PageIndex; i++)
{
if (i >= mDataPage.PageIndex || i<0)
continue;
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,i,urlparam) %>" ><%=i + 1%></a>
</td>
<%} %>
<td valign="middle">
<a href="javascript:void(0)" search=""><b><%=mDataPage.PageIndex+1%></b></a>
</td>
<%for(int i =mDataPage.PageIndex+1;i<mDataPage.PageCount;i++){
if (i >= mDataPage.PageIndex + 3)
break;
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,i,urlparam) %>" ><%=i+1 %></a>
</td>
<%} %>
<%
spitindex = mDataPage.PageIndex + 3;
if (mDataPage.PageCount -4 > spitindex){%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,spitindex+2,urlparam) %>" >
![](https://www.cnblogs.com/Images/dot.gif)
</td>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,mDataPage.PageCount -1,urlparam) %>" ><%=mDataPage.PageCount%></a>
</td>
<%}
else{
for (int i = spitindex; i <mDataPage.PageCount ; i++)
{
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,i,urlparam) %>" ><%=i + 1%></a>
</td>
<% }
}%>
<%
if(mDataPage.PageIndex !=mDataPage.PageCount-1)
{
%>
<td valign="middle">
<a search="" href="<%=HtmlHelper.WriteDataPage(mDataPage,null,mDataPage.PageIndex+1,urlparam) %>" >
>></a>
</td>
<%} %>
</tr>
</table>
用这种方式的好处在于页面制作人员很方便地根据实际需要进行式样制定,如果所有表现都交给代码来处理的,调分页表现方式修改起来就相对麻烦一点了;毕竟修改代码并不是页面制作人员的专长(其实很多都不懂,也不需要懂)。对于如何使用呢,把控件拖到页面就可以了。可能会有朋友问这个控件是怎样起作用的呢?其实只要相关Action提供IDataPage类型的datapage属性这个控件就能工作起来。
基于URL的分页方式当然不能只有分页描述,因为很多时候分页的URL里还必须包括相关查询内容提供。下面定义一个URL参数对象把信息附加到分页的URL里。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public interface IUrlParams
{
string GetParams();
}
[Serializable]
public class UrlParams : IUrlParams
{
public const string DATA_TAG = "URLParams";
#region IDataPageParamUrl 成员
protected Dictionary<string, string> mProperties = new Dictionary<string, string>();
public void Add(string key, string value)
{
mProperties.Add(key, value);
}
public string GetParams()
{
if (mProperties.Count == 0)
return "";
System.Text.StringBuilder sb = new StringBuilder();
string value;
foreach (string key in mProperties.Keys)
{
value = mProperties[key];
sb.Append(string.Format("{0}={1}&", key, value == null ? "" : System.Web.HttpUtility.UrlEncode(value)));
}
return sb.ToString();
}
#endregion
}
为了能够更方便的输URL信息,再定义一些简单辅助输出方法.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static string WriteDataPage(string order)
{
IDataPage mDataPage = MVCContext.Current.GetData<IDataPage>("DataPage");
if (mDataPage == null)
mDataPage = new DataPage();
return WriteDataPage(mDataPage, order, mDataPage.PageIndex, MVCContext.Current.GetData<IUrlParams>(UrlParams.DATA_TAG));
}
public static string WriteDataPage(IDataPage info, string order, int? index)
{
return WriteDataPage(info, order, index, null);
}
public static string WriteDataPage(IDataPage info, string order, int? index, IUrlParams geturl)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string paramurl;
HttpRequest request = HttpContext.Current.Request;
HttpResponse response = HttpContext.Current.Response;
string orderfield = "";
sb.Append(request.FilePath);
if (index != null)
{
sb.Append("?PageIndex=" + index);
}
else
{
sb.Append("?PageIndex=" + info.PageIndex);
}
sb.Append("&PageSize=" + info.PageSize);
if (order == null || order == "")
{
orderfield = info.OrderField;
}
else
{
if (info.OrderField == null || info.OrderField == "")
{
orderfield = order + " asc";
}
else
{
if (info.OrderField.IndexOf(order + " asc") >= 0)
{
orderfield = order + " desc";
}
else
{
orderfield = order + " asc";
}
}
}
if (orderfield != "")
{
sb.Append("&OrderField=" + HttpUtility.UrlEncode(orderfield));
}
if (geturl != null)
{
paramurl = geturl.GetParams();
if (paramurl != null && paramurl != "")
{
sb.Append("&" + geturl.GetParams());
}
}
return sb.ToString();
}
效果图
是不是有点难看呢:)既然难看就扔给制作人员把它美化一下吧,说不定美化后结果很好看的哦。