asp.net webform自定义分页控件(2)

<div> text: <asp:TextBox runat="server"></asp:TextBox> <asp:Button runat="server" Text="查 询"/> </div> <div> <asp:GridView runat="server" AutoGenerateColumns="true"></asp:GridView> <uc1:PagerControl runat="server" /> </div>

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

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<string>("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中注册翻页后的事件。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdppfj.html