首页 > 开发 > 综合 > 正文

Repeater控件的分页问题

2024-07-21 02:17:18
字体:
来源:转载
供稿:网友

以前做asp的时间不算短,可是做asp.net我是个新手。前几天做项目,遇到一个问题,要求比较复杂的数据格式显示,用datagrid非常难实现,后来改用了repeater控件来实现。不过这样就带来一个新问题:怎么分页?repeater没有内置的分页功能。后来自己试着写了一个,侥幸成功了。匆匆写就,肯定不完善,哪位高手有何高见请及时指正,很愿意与各位交流([email protected])

protected system.web.ui.webcontrols.repeater rptframework



private void page_load(object sender, system.eventargs e)
{



//建立dataset
(该部分省略)

rptframework.datasource = mydataset;
rptframework.databind();

session["page"] = 1;





//将repeater所有条目隐藏
int i = 0 ;

for( i = 0 ; i < rptframework.items.count ; i++ )
{
rptframework.items[i].visible = false;
}



//repeater控件的页面大小
int intpagesize = 10;






//rptframework控件的当前页索引(1起始)
int intcurrentpageindex = 0 ;



if ( request.querystring["page"] == null || convert.toint32( request.querystring["page"] ) == 0 )
{
session["page"] = 1 ;
}
else
{
session["page"] = convert.toint32( request.querystring["page"] );
}

intcurrentpageindex = convert.toint32( session["page"] );



//上一页按钮屏蔽
if ( convert.toint32( session["page"] ) == 1 )
{
btnpre.enabled = false;
}



//rptframework控件的当前游标索引(1起始)
int intcurrentitemindex = 1 ;
intcurrentitemindex = intpagesize * ( intcurrentpageindex - 1 ) + 1 ;



//下一页按钮屏蔽
if ( (rptframework.items.count - intcurrentitemindex) < intpagesize )
{
btnnext.enabled = false;
}




//设置窗体加载时的默认视图
if ( rptframework.items.count > intpagesize )
{
for (i = intcurrentitemindex ; i < getmin( rptframework.items.count , intcurrentitemindex + intpagesize ) ; i++ )
{
rptframework.items[i-1].visible = true;
}


}



}




//取两个数值中较小的值,但是不改变原数值大小
private int getmin( int a , int b )
{
int c ;
if ( a > b )
{
c = b;
}
else
{
c = a;
}
return c;
}



//点击上一页按钮
private void btnpre_click(object sender, system.eventargs e)
{

int intprepageindex = 2;

if ( convert.toint32( session["page"] ) != 1 )
{
intprepageindex = convert.toint32( session["page"] ) - 1;
}
else
{
intprepageindex = 1;
}

response.redirect( "jggk.aspx?page=" + intprepageindex.tostring() );
}



//点击下一页按钮
private void btnnext_click(object sender, system.eventargs e)
{
int intnextpageindex = convert.toint32( session["page"] ) + 1;
response.redirect( "jggk.aspx?page=" + intnextpageindex.tostring() );
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表