如何使用asp.net web頁面自定義分頁控件?

ADVERTISEMENT

如何使用asp.net web頁面自定義分頁控件?

這幾天學習了一下自定義分頁控件,現將實現方法記錄下來,親測可以實現:

1.首先創建一個.ascx文件,命名為TurnPage,然後在里面寫控件前台展示的界面:

2.然後在TurnPage.ascx.cs里面寫相應的後台代碼,代碼如下:


 namespace Web
 {
 public delegate void GoToPage(int PageNum);
 
 public partial class TurnPage : System.Web.UI.UserControl
 {
 private GoToPage _GoToPage = null;
 
 protected void Page_Load(object sender, EventArgs e)
 {
 
 }
 
 public void InitControl(GoToPage GP)
 {
 _GoToPage = GP;
 }
 
 
 public int DataCount
 {
 get { return Int32.Parse(lbl_TotalCount.Text); }
 set { lbl_TotalCount.Text = value.ToString(); }
 }
 
 public int CurrPageNum
 {
 get { return Int32.Parse(lbl_CurrPage.Text); }
 set { lbl_CurrPage.Text = value.ToString(); }
 }
 
 public int TotalPageNum
 {
 get { return Int32.Parse(lbl_TotalPage.Text); }
 set { lbl_TotalPage.Text = value.ToString(); }
 }
 
 public int PageSize
 {
 get { return Int32.Parse(ddl_PageSize.SelectedValue); }
 }
 
 protected void btn_FristPage_Click(object sender, EventArgs e)
 {
 _GoToPage(1);
 }
 
 protected void btn_PrevPage_Click(object sender, EventArgs e)
 {
 if (int.Parse(lbl_CurrPage.Text) > 1)
 _GoToPage(int.Parse(lbl_CurrPage.Text) - 1);
 else
 _GoToPage(1);
 }
 
 protected void btn_NextPage_Click(object sender, EventArgs e)
 {
 if (int.Parse(lbl_CurrPage.Text) 0)
 {
 btn_FristPage.Enabled = true;
 btn_PrevPage.Enabled = true;
 btn_LastPage.Enabled = true;
 btn_NextPage.Enabled = true;
 }
 else
 {
 btn_FristPage.Enabled = false;
 btn_PrevPage.Enabled = false;
 btn_LastPage.Enabled = false;
 btn_NextPage.Enabled = false;
 }
 
 
 
 if (CurrPageNum == 1)
 {
 btn_FristPage.Enabled = false;
 btn_PrevPage.Enabled = false;
 }
 
 if (CurrPageNum == TotalPageNum)
 {
 btn_LastPage.Enabled = false;
 btn_NextPage.Enabled = false;
 }
 
 if (CurrPageNum == 0)
 {
 btn_FristPage.Enabled = false;
 btn_PrevPage.Enabled = false;
 btn_LastPage.Enabled = false;
 btn_NextPage.Enabled = false;
 }
 }
 
 protected void ddl_PageSize_SelectedIndexChanged(object sender, EventArgs e)
 {
 _GoToPage(1);
 }
 
 protected void btn_GO_Click(object sender, EventArgs e)
 {
 int pageNum;
 if (int.TryParse(txt_PageNum.Text, out pageNum))
 {
 if (pageNum > TotalPageNum)
 _GoToPage(TotalPageNum);
 else if (pageNum 

3.控件的方法就寫好了,現在要做的就是在其它頁面引用這個控件了,引用方法如下:

然後在頁尾還需配置一下:

4.這就是最後一步了,在你引用這個控件的頁面的後台代碼里寫下以下語句:

大功告成了!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持路飯。


本文地址:
ADVERTISEMENT