asp.net webform自定义分页控件

做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件。

翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册。

有图有真相,给个直观的认识:

asp.net webform自定义分页控件_第1张图片

自定义分页控件前台代码:


当前/ 条记录 首页 | 上一页 |下一页 |尾页 跳至1 条/每页

自定义分页控件后台代码:

private const string viewStateCurrentPageIndex = "CurrentPageIndex";
  private const string viewStateRecordCount = "RecordCount";

  public delegate void PageChangedHandle();
  public event PageChangedHandle OnPageChanged;

  public int PageSize
  {
   get
   {
    return Convert.ToInt32(ddlPageSize.SelectedValue);
   }
  }

  public int CurrentPageIndex
  {
   set
   {
    ViewState[viewStateCurrentPageIndex] = value;
   }
   get
   {
    if (ViewState[viewStateCurrentPageIndex] == null)
    {
     ViewState[viewStateCurrentPageIndex] = 1;
    }

    return Convert.ToInt32(ViewState[viewStateCurrentPageIndex]);
   }
  }
  public int RecordCount
  {
   get
   {
    if (ViewState[viewStateRecordCount] == null)
    {
     ViewState[viewStateRecordCount] = 0;
    }

    return Convert.ToInt32(ViewState[viewStateRecordCount]);
   }
   set
   {
    ViewState[viewStateRecordCount] = value;
   }
  }
  private int TotalNumberOfPages
  {
   get
   {
    return RecordCount % PageSize == 0 ? RecordCount / PageSize : (RecordCount / PageSize) + 1;
   }
  }

  protected void Page_Load(object sender, EventArgs e)
  {
   if (!IsPostBack)
   {

   }
  }

  protected void labFirstPage_Click(object sender, EventArgs e)
  {
   CurrentPageIndex = 1;

   this.DataBind();
  }

  protected void labPreviousPage_Click(object sender, EventArgs e)
  {
   CurrentPageIndex -= 1;

   this.DataBind();
  }

  protected void labNextPage_Click(object sender, EventArgs e)
  {
   CurrentPageIndex += 1;

   this.DataBind();
  }

  protected void labLastPage_Click(object sender, EventArgs e)
  {
   CurrentPageIndex = TotalNumberOfPages;

   this.DataBind();
  }

  protected void btnGo_Click(object sender, EventArgs e)
  {
   int pageNum = 1;
   bool isNum = Int32.TryParse(txtPageNum.Text, out pageNum);
   if (isNum)
   {
    CurrentPageIndex = Math.Min(pageNum, TotalNumberOfPages);
   }

   this.DataBind();
  }

  protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
  {
   CurrentPageIndex = 1;

   this.DataBind();
  }

  protected override void DataBind(bool raiseOnDataBinding)
  {
   BindPager();
   base.DataBind(raiseOnDataBinding);

   if (OnPageChanged != null)
   {
    OnPageChanged();
   }
  }

  private void BindPager()
  {
   labCurrentPageIndex.Text = CurrentPageIndex.ToString();
   labTotalNumberOfPages.Text = TotalNumberOfPages.ToString();
   labRecordCount.Text = RecordCount.ToString();

   SetNavigateEnabled();
  }  

  private void SetNavigateEnabled()
  {
   txtPageNum.Text = CurrentPageIndex.ToString();
   labFirstPage.Enabled = true;
   labPreviousPage.Enabled = true;
   labNextPage.Enabled = true;
   labLastPage.Enabled = true;

   labFirstPage.CssClass = "font-blue";
   labPreviousPage.CssClass = "font-blue";
   labNextPage.CssClass = "font-blue";
   labLastPage.CssClass = "font-blue";

   if (CurrentPageIndex == 1)
   {
    labFirstPage.Enabled = false;
    labPreviousPage.Enabled = false;

    labFirstPage.CssClass = "gray";
    labPreviousPage.CssClass = "gray";
   }
   if (CurrentPageIndex == TotalNumberOfPages)
   {
    labNextPage.Enabled = false;
    labLastPage.Enabled = false;

    labNextPage.CssClass = "gray";
    labLastPage.CssClass = "gray";
   }
   if (RecordCount == 0)
   {
    labFirstPage.Enabled = false;
    labPreviousPage.Enabled = false;

    labFirstPage.CssClass = "gray";
    labPreviousPage.CssClass = "gray";

    labNextPage.Enabled = false;
    labLastPage.Enabled = false;

    labNextPage.CssClass = "gray";
    labLastPage.CssClass = "gray";
   }
  }

当前页码、总共多少条记录使用ViewState记录状态信息,因为导航控件会引起回发刷新。
分页后的数据加载,使用事件。

事件的具体实现放在使用分页控件的具体页面中,进行事件的注册。

测试分页控件的前台页面:

text:

测试分页控件的后台代码:

 private const string dtSourceViewStateKey = "dtSourceViewStateKey";

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
  BindData(true);

  }
  Pager.OnPageChanged += OnPageChanged;
 }

 private void BindData(bool bindRecordCount)
 {
  DataTable dtSource = GetDataSource();
  

  var source = dtSource.AsEnumerable();
  if (!string.IsNullOrEmpty(txtContent.Text.Trim()))
  {
  source = source.Where(w => w.Field("text").Contains(txtContent.Text.Trim()));
  }

  if (bindRecordCount)
  {
  Pager.RecordCount = source.Count();
  Pager.CurrentPageIndex = 1;
  Pager.DataBind();
  }

  gvList.DataSource = source
  .Skip((Pager.CurrentPageIndex - 1) * Pager.PageSize)
  .Take(Pager.PageSize)
  .Select(r => new { id = r["id"].ToString(), text = r["text"].ToString() })
  .ToList();
  gvList.DataBind();
 }

 private void OnPageChanged()
 {
  BindData(false);
 }

 private DataTable InitDataTable()
 {
  DataTable dtSource = new DataTable();
  DataColumn id = new DataColumn("id");
  id.AutoIncrement = true;
  id.AutoIncrementSeed = 1;

  dtSource.Columns.Add(id);
  dtSource.Columns.Add("text");

  for (int i = 1; i <= 1000; i++)
  {
  DataRow dr = dtSource.NewRow();
  dr["text"] = "内容" + i;

  dtSource.Rows.Add(dr);
  }

  return dtSource;
 }

 private DataTable GetDataSource()
 {
  if (ViewState[dtSourceViewStateKey] == null)
  {
  ViewState[dtSourceViewStateKey] = InitDataTable();
  }

  return ViewState[dtSourceViewStateKey] as DataTable;
 }

 protected void btnQuery_Click(object sender, EventArgs e)
 {
  BindData(true);
 }

在Page_Load中注册翻页后的事件。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(asp.net webform自定义分页控件)