asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET 翻页后继续维持排序


发布日期:2020年11月15日
 
ASP.NET 翻页后继续维持排序

要想实现翻页后继续排序实现这样的效果:

比如共 笔记录每页显示

则排序时:第一页将前条记录排序翻第二页时后五条再单独排序

要注意以下几点:

如果数据很多最好不要把数据集放到缓存中(viewstate)影响性能

ViewState中存放上次是哪个eexpression并且存放此eexpression是升序还是降序

示例如下:

现有的排序事件是这样写的这个是点击上面排序标题时用:

private void grdProjTrace_SortCommand(object source DataGridSortCommandEventArgs e)

{

thisgrdProjTraceCurrentPageIndex = ;

DataView dv = 得到数据代码;

string strSort = ;

string strOrder =;//排序方式降序升序

if(ViewState[SortExpresstion] != null)

{

strSort = ViewState[SortExpresstion]ToString();

strSort = strSortSubstring(strSortLength );

strOrder = ViewState[SortExpresstion]ToString();

strOrder = strOrderSubstring(strOrderLength );

}

if(eSortExpression == CustomerName)

{

if(strSort != CustomerName)

{

thisViewState[SortExpresstion] = ustomerName;

dvSort = CustomerName DESC;

}

else

{

if(strOrder == )

{

thisViewState[SortExpresstion] = CustomerName;

dvSort = CustomerName ASC;

}

else

{

thisViewState[SortExpresstion] = CustomerName;

dvSort = CustomerName DESC;

}

}

}

if(eSortExpression == fullName)

{

if(strSort != fullName)

{

thisViewState[SortExpresstion] = fullName;

dvSort = fullName DESC;

}

else

{

if(strOrder == )

{

thisViewState[SortExpresstion] = fullName;

dvSort = fullName ASC;

}

else

{

thisViewState[SortExpresstion] = fullName;

dvSort = fullName DESC;

}

}

}

thisgrdProjTraceDataSource = dv;

thisgrdProjTraceDataBind();

} >

下面这个方法是自己写的翻页事件中调用

private void ChangePageDataBind()

{

DataView dv = 得到数据代码;

string strSort = ;

string strOrder =;//排序方式降序升序

if(ViewState[SortExpresstion] != null)

{

strSort = ViewState[SortExpresstion]ToString();

strSort = strSortSubstring(strSortLength );

strOrder = ViewState[SortExpresstion]ToString();

strOrder = strOrderSubstring(strOrderLength );

}

if(thisViewState[SortExpresstion] != null)

{

if(strSort == CustomerName)

{

if(strOrder == )

{

thisViewState[SortExpresstion] = CustomerName;

dvSort = CustomerName ASC;

}

else

{

thisViewState[SortExpresstion] = CustomerName;

dvSort = CustomerName DESC;

}

}

}

if(thisViewState[SortExpresstion] != null)

{

if(strSort == fullName)

{

if(strOrder == )

{

thisViewState[SortExpresstion] = fullName;

dvSort = fullName ASC;

}

else

{

thisViewState[SortExpresstion] = fullName;

dvSort = fullName DESC;

}

}

}

thisgrdProjTraceDataSource = dv;

thisgrdProjTraceDataBind();

}

上面两方法只要修改要排序的字段名就可以直接调用了

方法很简单实用这里就不说了

方法是这样用的:

private void grdProjTrace_PageIndexChanged(object source DataGridPageChangedEventArgs e)

{

try

{

try

{

thisgrdProjTraceCurrentPageIndex = eNewPageIndex;

}

catch

{

thisgrdProjTraceCurrentPageIndex = ;

}

thisChangePageDataBind();

}

catch(SystemException errWS)

{

//异常

}

}                

上一篇:asp.net怎么将网页添加为首页或加入收藏夹中

下一篇:运用ASP.NET读写Windows注册编辑表