本篇描述如何使用GridView(需要实现IPageableItemContainer接口)加DataPager实现分页功能。
并使用ObjectDataSource控件来获得数据源实现分页。
1,ObjectDataSource属性设置
SelectMethod:获得分页的数据源。将获得数据源的方法名赋给该属性。
至少传入2个参数,起始页startRowIndex,每页显示页数maximumRows。
SelectCountMethod:获得总数据行数。将获得数据源总行数的方法名赋给该属性。
注意:方法的返回类型必须是int类型(或小于该类型的),不然会
GridView出现容量不足异常(gridview不会抛出此异常,Listview会抛此异常)。
StartRowIndexParameterName:起始页参数,对应方法参数名startRowIndex
MaximumRowsParameterName:每页显示数参数,对应方法参数名maximumRows
EnablePaging:设置为"True"
其它查询参数可以根据情况在SelectParameters中增加相应查询条件参数。
2,DataPager属性设置
PageSize:设置分页的大小,分页时会将该属性的值传递给maximumRows
PagedControlID:分页的控件(GridView,ListView)的ID。
3,GridView属性设置
AllowPaging:设置为"True"
DataSourceID:ObjectDataSource的ID
4,Nhibernate分页方法:
example:
public IList<ExceptionLog> GetExceptionLogByQuery(DateTime loTime, DateTime hiTime, string appId, int startRowIndex, int maximumRows)
{
var result = this.Session.GetISession().CreateCriteria<ExceptionLog>()
.Add(Restrictions.Between("CreateTime",loTime,hiTime))
.Add(Restrictions.Eq("appId", appId))
.SetMaxResults(maximumRows)
.SetFirstResult(startRowIndex).List<ExceptionLog>();
return result;
}
public int GetExceptionLogByQueryCount(DateTime loTime,DateTime hiTime, string appId)
{
var result = this.Session.GetISession().CreateCriteria<ExceptionLog>()
.Add(Restrictions.Between("CreateTime", loTime, hiTime))
.Add(Restrictions.Eq("appId", appId))
.SetProjection(Projections.RowCount()).UniqueResult<int>();
return result;
}
5,例子
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
DataSourceID="ObjectDataSource1">
</asp:GridView>
<asp:DataPager ID="pager" runat="server" PageSize="8"
PagedControlID="GridView1">
<Fields>
<asp:NextPreviousPagerField ButtonCssClass="command" FirstPageText="«" PreviousPageText="‹"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="true" ShowPreviousPageButton="true"
ShowLastPageButton="false" ShowNextPageButton="false" />
<asp:NumericPagerField ButtonCount="7" NumericButtonCssClass="command" CurrentPageLabelCssClass="current"
NextPreviousButtonCssClass="command" />
<asp:NextPreviousPagerField ButtonCssClass="command" LastPageText="»" NextPageText="›"
RenderDisabledButtonsAsLabels="true" ShowFirstPageButton="false" ShowPreviousPageButton="false"
ShowLastPageButton="true" ShowNextPageButton="true" />
</Fields>
</asp:DataPager>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetExceptionLogByQuery"
SelectCountMethod="GetExceptionLogByQueryCount" TypeName="BLL.ExceptionLog"
StartRowIndexParameterName="startRowIndex" MaximumRowsParameterName="maximumRows"
EnablePaging="True">
<SelectParameters>
<asp:ControlParameter ControlID="txtLowTime" Name="loTime" PropertyName="Text" Type="DateTime"
DefaultValue="2000-1-1" />
<asp:ControlParameter ControlID="txtHiTime" Name="hiTime" PropertyName="Text" Type="DateTime"
DefaultValue="2010-11-1" />
<asp:ControlParameter ControlID="txtUserId" Name="userId" PropertyName="Text" Type="String"
DefaultValue="joe" />
</SelectParameters>
</asp:ObjectDataSource>
实现接口:
/// <summary>
///
/// </summary>
[ToolboxData("<{0}:GridView runat=server></{0}:GridView>")]
public class GridView : System.Web.UI.WebControls.GridView, IPageableItemContainer
{
/// <summary>
/// TotalRowCountAvailable event key
/// </summary>
private static readonly object EventTotalRowCountAvailable = new object();
/// <summary>
///
/// </summary>
/// <param name="dataSource"></param>
/// <param name="dataBinding"></param>
/// <returns></returns>
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int rows = base.CreateChildControls(dataSource, dataBinding);
// if the paging feature is enabled, determine
// the total number of rows in the datasource
if (this.AllowPaging)
{
// if we are databinding, use the number of rows that were created,
// otherwise cast the datasource to an Collection and use that as the count
int totalRowCount = dataBinding ? rows : ((ICollection)dataSource).Count;
// raise the row count available event
IPageableItemContainer pageableItemContainer = this as IPageableItemContainer;
this.OnTotalRowCountAvailable(
new PageEventArgs(
pageableItemContainer.StartRowIndex,
pageableItemContainer.MaximumRows,
totalRowCount
)
);
// make sure the top and bottom pager rows are not visible
if (this.TopPagerRow != null)
{
this.TopPagerRow.Visible = false;
}
if (this.BottomPagerRow != null)
{
this.BottomPagerRow.Visible = false;
}
}
return rows;
}
#region IPageableItemContainer Interface
/// <summary>
///
/// </summary>
/// <param name="startRowIndex"></param>
/// <param name="maximumRows"></param>
/// <param name="databind"></param>
void IPageableItemContainer.SetPageProperties(
int startRowIndex, int maximumRows, bool databind)
{
int newPageIndex = (startRowIndex / maximumRows);
this.PageSize = maximumRows;
if (this.PageIndex != newPageIndex)
{
bool isCanceled = false;
if (databind)
{
// create the event args and raise the event
GridViewPageEventArgs args = new GridViewPageEventArgs(newPageIndex);
this.OnPageIndexChanging(args);
isCanceled = args.Cancel;
newPageIndex = args.NewPageIndex;
}
// if the event wasn't cancelled
// go ahead and change the paging values
if (!isCanceled)
{
this.PageIndex = newPageIndex;
if (databind)
{
this.OnPageIndexChanged(EventArgs.Empty);
}
}
if (databind)
{
this.RequiresDataBinding = true;
}
}
}
/// <summary>
///
/// </summary>
int IPageableItemContainer.StartRowIndex
{
get { return this.PageSize * this.PageIndex; }
}
/// <summary>
///
/// </summary>
int IPageableItemContainer.MaximumRows
{
get { return this.PageSize; }
}
/// <summary>
///
/// </summary>
event EventHandler<PageEventArgs> IPageableItemContainer.TotalRowCountAvailable
{
add { base.Events.AddHandler(GridView.EventTotalRowCountAvailable, value); }
remove { base.Events.RemoveHandler(GridView.EventTotalRowCountAvailable, value); }
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected virtual void OnTotalRowCountAvailable(PageEventArgs e)
{
EventHandler<PageEventArgs> handler = (EventHandler<PageEventArgs>)base.Events[GridView.EventTotalRowCountAvailable];
if (handler != null)
{
handler(this, e);
}
}
#endregion
}
reference:Using a DataPager with the GridView Control - Implementing IPageableItemContainer