分页|控件|问题以前做ASP的时间不算短,可是做ASP.NET我是个新手。前几天做项目,遇到一个问题,要求比较复杂的数据格式显示,用DataGrid非常难实现,后来改用了Repeater控件来实现。不过这样就带来一个新问题:怎么分页?Repeater没有内置的分页功能。后来自己试着写了一个,侥幸成功了。匆匆写就,肯定不完善,哪位高手有何高见请及时指正,很愿意与各位交流(zhoubinmail.sdu.edu.cn)
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() );
}
Repeater控件的分页问题
80酷酷网 80kuku.com