void CreateNextPrevPager(TableRow row, PagedDataSourceEx pagedDataSource, bool addFirstLastPageButtons) { PagerSettingsEx pagerSettings = this.PagerSettings; string previousPageImageUrl = pagerSettings.PreviousPageImageUrl; string nextPageImageUrl = pagerSettings.NextPageImageUrl; bool isFirstPage = pagedDataSource.IsFirstPage; bool isLastPage = pagedDataSource.IsLastPage; #region 第一页 if (addFirstLastPageButtons && !isFirstPage) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); string firstPageImageUrl = pagerSettings.FirstPageImageUrl; if (!string.IsNullOrEmpty(firstPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = firstPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.FirstPageText); ((DataControlImageButton)control).ToolTip = "第一页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(0)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.FirstPageText; ((DataControlPagerLinkButton)control).ToolTip = "第一页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(0)); } control.CommandName = "Page"; control.CommandArgument = "First"; cell.Controls.Add((Control)control); } #endregion #region 上一页 if (!isFirstPage) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); if (!string.IsNullOrEmpty(previousPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = previousPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.PreviousPageText); ((DataControlImageButton)control).ToolTip = "上一页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(this.PageIndex - 1)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.PreviousPageText; ((DataControlPagerLinkButton)control).ToolTip = "上一页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(this.PageIndex - 1)); } control.CommandName = "Page"; control.CommandArgument = "Prev"; cell.Controls.Add((Control)control); } #endregion #region 下一页 if (!isLastPage) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); if (!string.IsNullOrEmpty(nextPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = nextPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.NextPageText); ((DataControlImageButton)control).ToolTip = "下一页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(this.PageIndex + 1)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.NextPageText; ((DataControlPagerLinkButton)control).ToolTip = "下一页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(this.PageIndex + 1)); } control.CommandName = "Page"; control.CommandArgument = "Next"; cell.Controls.Add((Control)control); } #endregion #region 最后页 if (addFirstLastPageButtons && !isLastPage) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); string lastPageImageUrl = pagerSettings.LastPageImageUrl; if (!string.IsNullOrEmpty(lastPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = lastPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.LastPageText); ((DataControlImageButton)control).ToolTip = "最后页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(pagedDataSource.PageCount - 1)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.LastPageText; ((DataControlPagerLinkButton)control).ToolTip = "最后页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(pagedDataSource.PageCount - 1)); } control.CommandName = "Page"; control.CommandArgument = "Last"; cell.Controls.Add((Control)control); } #endregion }
void CreateNumericPager(TableRow row, PagedDataSourceEx pagedDataSource, bool addFirstLastPageButtons) { LinkButton button = null; PagerSettingsEx pagerSettings = this.PagerSettings; int pageCount = pagedDataSource.PageCount; int nextPageIndex = pagedDataSource.CurrentPageIndex + 1; int pageButtonCount = pagerSettings.PageButtonCount; int firstDisplayPageIndex = this.FirstDisplayPageIndex + 1; int pageIndex = (pageCount < pageButtonCount) ? pageCount : pageButtonCount; int startIndex = 1; if (nextPageIndex > pageIndex) { if (firstDisplayPageIndex > 0 && ((nextPageIndex - firstDisplayPageIndex >= 0) && (nextPageIndex - firstDisplayPageIndex < pageButtonCount))) startIndex = firstDisplayPageIndex; else startIndex = (pagedDataSource.CurrentPageIndex / pageButtonCount) + 1; pageIndex = startIndex + pageButtonCount - 1; if (pageIndex > pageCount) pageIndex = pageCount; if ((pageIndex - startIndex) + 1 < pageButtonCount) startIndex = Math.Max(1, (pageIndex - pageButtonCount + 1)); this.FirstDisplayPageIndex = startIndex - 1; } #region 首页 if (addFirstLastPageButtons && (nextPageIndex != 1) && (startIndex != 1)) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); string firstPageImageUrl = pagerSettings.FirstPageImageUrl; if (!string.IsNullOrEmpty(firstPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = firstPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.FirstPageText); ((DataControlImageButton)control).ToolTip = "第一页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(0)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.FirstPageText; ((DataControlPagerLinkButton)control).ToolTip = "第一页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(0)); } control.CommandName = "Page"; control.CommandArgument = "First"; cell.Controls.Add((Control)control); } #endregion #region 前更多 if (startIndex != 1) { TableCell cell = new TableCell(); row.Cells.Add(cell); button = new DataControlPagerLinkButton(this); button.Text = "..."; button.ToolTip = "更多..."; button.CommandName = "Page"; button.CommandArgument = (startIndex - 1).ToString(NumberFormatInfo.InvariantInfo); ((DataControlPagerLinkButton)button).EnableCallback(this.BuildCallbackArgument(startIndex - 2)); cell.Controls.Add(button); } #endregion #region 页索引 for (int i = startIndex; i <= pageIndex; i++) { TableCell cell = new TableCell(); row.Cells.Add(cell); string str = i.ToString(NumberFormatInfo.InvariantInfo); if (i == nextPageIndex) { Label child = new Label(); child.Text = str; cell.Controls.Add(child); } else { button = new DataControlPagerLinkButton(this); button.Text = str; button.ToolTip = str; button.CommandName = "Page"; button.CommandArgument = str; ((DataControlPagerLinkButton)button).EnableCallback(this.BuildCallbackArgument(i - 1)); cell.Controls.Add(button); } } #endregion #region 后更多 if (pageCount > pageIndex) { TableCell cell = new TableCell(); row.Cells.Add(cell); button = new DataControlPagerLinkButton(this); button.Text = "..."; button.ToolTip = "更多..."; button.CommandName = "Page"; button.CommandArgument = (pageIndex + 1).ToString(NumberFormatInfo.InvariantInfo); ((DataControlPagerLinkButton)button).EnableCallback(this.BuildCallbackArgument(pageIndex)); cell.Controls.Add(button); } #endregion #region 末页 if (addFirstLastPageButtons && (nextPageIndex != pageCount) && (pageIndex != pageCount)) { IButtonControl control = null; TableCell cell = new TableCell(); row.Cells.Add(cell); string lastPageImageUrl = pagerSettings.LastPageImageUrl; if (!string.IsNullOrEmpty(lastPageImageUrl)) { control = new DataControlImageButton(this); ((DataControlImageButton)control).ImageUrl = lastPageImageUrl; ((DataControlImageButton)control).AlternateText = HttpUtility.HtmlDecode(pagerSettings.LastPageText); ((DataControlImageButton)control).ToolTip = "最后页"; ((DataControlImageButton)control).EnableCallback(this.BuildCallbackArgument(pagedDataSource.PageCount - 1)); } else { control = new DataControlPagerLinkButton(this); ((DataControlPagerLinkButton)control).Text = pagerSettings.LastPageText; ((DataControlPagerLinkButton)control).ToolTip = "最后页"; ((DataControlPagerLinkButton)control).EnableCallback(this.BuildCallbackArgument(pagedDataSource.PageCount - 1)); } control.CommandName = "Page"; control.CommandArgument = "Last"; cell.Controls.Add((Control)control); } #endregion }
/// <summary> /// 初始化在分页功能启用时显示的页导航行。 /// </summary> /// <param name="row">表示要初始化的页导航行。</param> /// <param name="columnSpan">页导航行应跨越的列数。</param> /// <param name="pagedDataSource">数据源。</param> protected virtual void InitializePager(DataGridViewRow row, int columnSpan, PagedDataSourceEx pagedDataSource) { TableCell cell = new TableCell(); if (columnSpan > 1) cell.ColumnSpan = columnSpan; PagerSettingsEx pagerSettings = this.PagerSettings; Panel panel = new Panel(); panel.CssClass = this.PagingDataCss; Table child = new Table(); child.CellPadding = 0; child.CellSpacing = 0; child.BorderWidth = 0; TableRow tr = new TableRow(); tr.HorizontalAlign = HorizontalAlign.Left; switch (pagerSettings.Mode) { case PagerButtons.NextPrevious: this.CreateNextPrevPager(tr, pagedDataSource, false); break; case PagerButtons.Numeric: this.CreateNumericPager(tr, pagedDataSource, false); break; case PagerButtons.NextPreviousFirstLast: // this.CreateNextPrevPager(tr, pagedDataSource, true); this.CreateDefaultNextPreviousFirstLastPager(tr, pagedDataSource); break; case PagerButtons.NumericFirstLast: this.CreateNumericPager(tr, pagedDataSource, true); break; } child.Rows.Add(tr); panel.Controls.Add(child); cell.Controls.Add(panel); //导出 this.CreateExportControl(cell); row.Cells.Add(cell); }
/// <summary> /// 创建默认分页 /// </summary> /// <param name="row"></param> /// <param name="pagedDataSource"></param> protected virtual void CreateDefaultNextPreviousFirstLastPager(TableRow row, PagedDataSourceEx pagedDataSource) { PagerSettingsEx pagerSettings = this.PagerSettings; bool isFirstPage = pagedDataSource.IsFirstPage; bool isLastPage = pagedDataSource.IsLastPage; TableCell cell = null; if (this.AllowPaging) { //第一页。 this.CreatePagerButton(row, pagerSettings.FirstPageImageUrl, "第一页", 0, "First", isFirstPage); //上一页 if (!isFirstPage) this.CreatePagerButton(row, pagerSettings.PreviousPageImageUrl, "上一页", this.PageIndex - 1, "Prev", isFirstPage); //下一页 if (!isLastPage) this.CreatePagerButton(row, pagerSettings.NextPageImageUrl, "下一页", this.PageIndex + 1, "Next", isLastPage); //最末页 this.CreatePagerButton(row, pagerSettings.LastPageImageUrl, "最末页", this.PageCount + 1, "Last", isLastPage); //页面统计。 cell = new TableCell(); cell.Attributes["nowrap"] = "true"; HtmlGenericControl span = new HtmlGenericControl("span"); span.Attributes["style"] = "float:left;margin-left:2px;"; span.InnerText = string.Format("第{0}页/共{1}页 总记录数:{2}", pagedDataSource.CurrentPageIndex + 1, pagedDataSource.PageCount, pagedDataSource.DataSourceCount); cell.Controls.Add(span); row.Cells.Add(cell); //页面跳转。 cell = new TableCell(); cell.Attributes["nowrap"] = "true"; HtmlGenericControl div = new HtmlGenericControl("div"); div.Attributes["class"] = this.FooterInputCSS; span = new HtmlGenericControl("span"); span.Attributes["style"] = "float:left;margin-left:2px;margin-top:3px;"; span.InnerText = "到第"; div.Controls.Add(span); LinkButton linkBtn = new DataControlPagerLinkButton(this); linkBtn.Attributes["style"] = "float:left;margin-top:2px;"; linkBtn.Text = "[GO]"; linkBtn.CommandName = "Page"; linkBtn.CommandArgument = (pagedDataSource.CurrentPageIndex + 1).ToString(NumberFormatInfo.InvariantInfo); ((DataControlPagerLinkButton)linkBtn).EnableCallback(this.BuildCallbackArgument(pagedDataSource.CurrentPageIndex)); TextBox numTextBox = new TextBox(); numTextBox.Text = (pagedDataSource.CurrentPageIndex + 1).ToString(NumberFormatInfo.InvariantInfo); numTextBox.Attributes["style"] = "float:left;"; numTextBox.Attributes["onkeypress"] = "javascript:return event.keyCode>=48&&event.keyCode<=57;"; numTextBox.Attributes["ondragenter"] = "javascript:return false;"; numTextBox.Attributes["onpaste"] = "javascript:return !clipboardData.getData('text').match(/\\D/);"; numTextBox.Style.Add("ime-mode", "Disabled"); numTextBox.MaxLength = 5; numTextBox.AutoPostBack = true; numTextBox.TextChanged += new EventHandler(delegate(object sender, EventArgs e) { int pageNo = 1, pageCount = pagedDataSource.PageCount; try { pageNo = int.Parse(((TextBox)sender).Text); } catch { } if (pageNo > pageCount) pageNo = pageCount; linkBtn.CommandArgument = pageNo.ToString(NumberFormatInfo.InvariantInfo); ((DataControlPagerLinkButton)linkBtn).EnableCallback(this.BuildCallbackArgument(pageNo - 1)); }); div.Controls.Add(numTextBox); span = new HtmlGenericControl("span"); span.Attributes["style"] = "float:left;margin-top:3px;"; span.InnerText = "页"; div.Controls.Add(span); div.Controls.Add(linkBtn); cell.Controls.Add(div); row.Cells.Add(cell); } else { cell = new TableCell(); cell.Controls.Add(new LiteralControl(string.Format(" 总记录数:{0}", pagedDataSource.DataSourceCount))); row.Cells.Add(cell); } cell = new TableCell(); cell.Attributes["align"] = "right"; LinkButton pagerLinkBtn = new LinkButton(); pagerLinkBtn.Text = this.AllowPaging ? "[全部]" : "[分页]"; pagerLinkBtn.CssClass = this.FooterLinkCSS; pagerLinkBtn.CausesValidation = false; pagerLinkBtn.Command += new CommandEventHandler(delegate(object sender, CommandEventArgs e) { this.AllowPaging = !this.AllowPaging; this.InvokeBuildDataSource(); }); cell.Controls.Add(pagerLinkBtn); row.Cells.Add(cell); }
/// <summary> /// 创建用来构建控件层次结构的列字段集。 /// </summary> /// <param name="dataSource">表示数据源。</param> /// <param name="useDataSource">true 表示使用 dataSource 参数指定的数据源;否则为 false。</param> /// <returns></returns> protected virtual ICollection CreateColumns(PagedDataSourceEx dataSource, bool useDataSource) { ArrayList list = new ArrayList(); foreach (DataControlFieldEx field in this.Columns) { list.Add(field); } return list; }
PagedDataSourceEx CreateServerPagedDataSource(int totalRowCount) { PagedDataSourceEx source = new PagedDataSourceEx(); source.CurrentPageIndex = this.PageIndex; source.PageSize = this.PageSize; source.AllowPaging = this.AllowPaging; source.AllowCustomPaging = false; source.AllowServerPaging = true; source.VirtualCount = totalRowCount; return source; }
DataGridViewRow CreateRow(int rowIndex, int dataSourceIndex, DataGridViewRowType rowType, DataGridViewRowState rowState, bool dataBind, object dataItem, DataControlFieldEx[] fields, TableRowCollection rows, PagedDataSourceEx pagedDataSource) { DataGridViewRow row = this.CreateRow(rowIndex, dataSourceIndex, rowType, rowState); DataGridViewRowEventArgs e = new DataGridViewRowEventArgs(row); if (rowType != DataGridViewRowType.Footer) this.InitializeRow(row, fields); else this.InitializePager(row, fields.Length, pagedDataSource); if (dataBind) row.DataItem = dataItem; this.OnRowCreated(e); rows.Add(row); if (dataBind) { row.DataBind(); this.OnRowDataBound(e); row.DataItem = null; } return row; }