以前做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() );