📄 webpager.cs
字号:
#endregion
}
#endregion
#region OraclePager Control
[DefaultProperty("SelectCommand")]
[DefaultEvent("PageIndexChanged")]
[ToolboxData("<{0}:OraclePager runat=\"server\" />")]
public class OraclePager : WebControl, INamingContainer
{
#region PRIVATE DATA MEMBERS
// ***********************************************************************
// PRIVATE members
private PagedDataSource _dataSource;
private Control _controlToPaginate;
private string CacheKeyName
{
get {return Page.Request.FilePath + "_" + UniqueID + "_Data";}
}
private string CurrentPageText = "总记录:<font color=#ff0000><B>{2}</B></font>  页次:<font color=#ff0000><B>{0}</B></font>/<B>{1}</B>页  <B>{3}</B>/页";
private string NoPageSelectedText = "";
private string QueryPageCommandText = "SELECT * FROM " +
"(SELECT t0.*,ROWNUM AS count_id FROM " +
"({2}) t0) t1 " +
"WHERE count_id BETWEEN {1}-{6}+1 " +
"AND {1}";
private string QueryCountCommandText = "SELECT COUNT(*) FROM ({0}) t0";
// ***********************************************************************
#endregion
#region CTOR(s)
// ***********************************************************************
// Ctor
public OraclePager() : base()
{
_dataSource = null;
_controlToPaginate = null;
PagingMode = PagingMode.Cached;
PagerStyle = PagerStyle.NextPrev;
CurrentPageIndex = 0;
SelectCommand = "";
ConnectionString = "";
ItemsPerPage = 10;
TotalPages = -1;
TotalRecord = -1;
CacheDuration = 60;
}
// ***********************************************************************
#endregion
#region PUBLIC PROGRAMMING INTERFACE
// ***********************************************************************
// METHOD ClearCache
// Removes any data cached for paging
public void ClearCache()
{
if (PagingMode == PagingMode.Cached)
Page.Cache.Remove(CacheKeyName);
}
// ***********************************************************************
// ***********************************************************************
// EVENT PageIndexChanged
// Fires when the pager is about to switch to a new page
public delegate void PageChangedEventHandler(object sender, PageChangedEventArgs e);
public event PageChangedEventHandler PageIndexChanged;
protected virtual void OnPageIndexChanged(PageChangedEventArgs e)
{
if (PageIndexChanged != null)
PageIndexChanged(this, e);
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY CacheDuration
[Description("Gets and sets for how many seconds the data should stay in the cache")]
public int CacheDuration
{
get {return Convert.ToInt32(ViewState["CacheDuration"]);}
set {ViewState["CacheDuration"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY PagingMode
[Description("Indicates whether the data are retrieved page by page or can be cached")]
public PagingMode PagingMode
{
get {return (PagingMode) ViewState["PagingMode"];}
set {ViewState["PagingMode"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY PagerStyle
[Description("Indicates the style of the pager's navigation bar")]
public PagerStyle PagerStyle
{
get {return (PagerStyle) ViewState["PagerStyle"];}
set {ViewState["PagerStyle"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY ControlToPaginate
[Description("Gets and sets the name of the control to paginate")]
public string ControlToPaginate
{
get {return Convert.ToString(ViewState["ControlToPaginate"]);}
set {ViewState["ControlToPaginate"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY ItemsPerPage
[Description("Gets and sets the number of items to display per page")]
public int ItemsPerPage
{
get {return Convert.ToInt32(ViewState["ItemsPerPage"]);}
set {ViewState["ItemsPerPage"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY CurrentPageIndex
[Description("Gets and sets the index of the currently displayed page")]
public int CurrentPageIndex
{
get {return Convert.ToInt32(ViewState["CurrentPageIndex"]);}
set {ViewState["CurrentPageIndex"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY ConnectionString
[Description("Gets and sets the connection string to access the database")]
public string ConnectionString
{
get {return Convert.ToString(ViewState["ConnectionString"]);}
set {ViewState["ConnectionString"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY SelectCommand
[Description("Gets and sets the SQL query to get data")]
public string SelectCommand
{
get {return Convert.ToString(ViewState["SelectCommand"]);}
set {ViewState["SelectCommand"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY SortField
[Description("Gets and sets the sort-by field. It is mandatory in NonCached mode.)")]
public string SortField
{
get {return Convert.ToString(ViewState["SortKeyField"]);}
set {ViewState["SortKeyField"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY PageCount
// Gets the number of displayable pages
[Browsable(false)]
public int PageCount
{
get {return TotalPages;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY TotalPages
// Gets and sets the number of pages to display
protected int TotalPages
{
get {return Convert.ToInt32(ViewState["TotalPages"]);}
set {ViewState["TotalPages"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// PROPERTY TotalPages
// Gets and sets the number of pages to display
protected int TotalRecord
{
get {return Convert.ToInt32(ViewState["TotalRecord"]);}
set {ViewState["TotalRecord"] = value;}
}
// ***********************************************************************
// ***********************************************************************
// OVERRIDE DataBind
// Fetches and stores the data
public override void DataBind()
{
// Fires the data binding event
base.DataBind();
// Controls must be recreated after data binding
ChildControlsCreated = false;
// Ensures the control exists and is a list control
if (ControlToPaginate == "")
return;
_controlToPaginate = Page.FindControl(ControlToPaginate);
if (_controlToPaginate == null)
return;
if (!(_controlToPaginate is BaseDataList || _controlToPaginate is ListControl))
return;
// Ensures enough info to connect and query is specified
if (ConnectionString == "" || SelectCommand == "")
return;
// Fetch data
if (PagingMode == PagingMode.Cached)
FetchAllData();
else
{
//if (SortField == "")
// return;
FetchPageData();
}
// Bind data to the buddy control
BaseDataList baseDataListControl = null;
ListControl listControl = null;
if (_controlToPaginate is BaseDataList)
{
baseDataListControl = (BaseDataList) _controlToPaginate;
baseDataListControl.DataSource = _dataSource;
baseDataListControl.DataBind();
return;
}
if (_controlToPaginate is ListControl)
{
listControl = (ListControl) _controlToPaginate;
listControl.Items.Clear();
listControl.DataSource = _dataSource;
listControl.DataBind();
return;
}
}
// ***********************************************************************
// ***********************************************************************
// OVERRIDE Render
// Writes the content to be rendered on the client
protected override void Render(HtmlTextWriter output)
{
// If in design-mode ensure that child controls have been created.
// Child controls are not created at this time in design-mode because
// there's no pre-render stage. Do so for composite controls like this
if (Site != null && Site.DesignMode)
CreateChildControls();
base.Render(output);
}
// ***********************************************************************
// ***********************************************************************
// OVERRIDE CreateChildControls
// Outputs the HTML markup for the control
protected override void CreateChildControls()
{
Controls.Clear();
ClearChildViewState();
BuildControlHierarchy();
}
// ***********************************************************************
#endregion
#region PRIVATE HELPER METHODS
// ***********************************************************************
// PRIVATE BuildControlHierarchy
// Control the building of the control's hierarchy
private void BuildControlHierarchy()
{
// Build the surrounding table (one row, two cells)
Table t = new Table();
t.Width = new Unit("100%");
// Build the table row
TableRow row = new TableRow();
t.Rows.Add(row);
// Build the cell with the page index
TableCell cellPageDesc = new TableCell();
cellPageDesc.HorizontalAlign = HorizontalAlign.Left;
BuildCurrentPage(cellPageDesc);
row.Cells.Add(cellPageDesc);
// Build the cell with navigation bar
TableCell cellNavBar = new TableCell();
if (PagerStyle == PagerStyle.NextPrev)
BuildNextPrevUI(cellNavBar);
else
BuildNumericPagesUI(cellNavBar);
cellNavBar.HorizontalAlign = HorizontalAlign.Right;
row.Cells.Add(cellNavBar);
// Add the table to the control tree
Controls.Add(t);
}
// ***********************************************************************
// ***********************************************************************
// PRIVATE BuildNextPrevUI
// Generates the HTML markup for the Next/Prev navigation bar
private void BuildNextPrevUI(TableCell cell)
{
bool isValidPage = (CurrentPageIndex >=0 && CurrentPageIndex <= TotalPages-1);
bool canMoveBack = (CurrentPageIndex>0);
bool canMoveForward = (CurrentPageIndex<TotalPages-1);
// Render the << button
LinkButton first = new LinkButton();
first.ID = "First";
first.Click += new EventHandler(first_Click);
first.Text = " 首页 ";
first.Enabled = isValidPage && canMoveBack;
cell.Controls.Add(first);
// Add a separator
cell.Controls.Add(new LiteralControl(" "));
// Render the < button
LinkButton prev = new LinkButton();
prev.ID = "Prev";
prev.Click += new EventHandler(prev_Click);
prev.Text = " 上页 ";
prev.Enabled = isValidPage && canMoveBack;
cell.Controls.Add(prev);
// Add a separator
cell.Controls.Add(new LiteralControl(" "));
// Render the > button
LinkButton next = new LinkButton();
next.ID = "Next";
next.Click += new EventHandler(next_Click);
next.Text = " 下页 ";
next.Enabled = isValidPage && canMoveForward;
cell.Controls.Add(next);
// Add a separator
cell.Controls.Add(new LiteralControl(" "));
// Render the >> button
LinkButton last = new LinkButton();
last.ID = "Last";
last.Click += new EventHandler(last_Click);
last.Text = " 尾页 ";
last.Enabled = isValidPage && canMoveForward;
cell.Controls.Add(last);
// Render a drop-down list
DropDownList pageList = new DropDownList();
pageList.ID = "PageList";
pageList.AutoPostBack = true;
pageList.SelectedIndexChanged += new EventHandler(PageList_Click);
pageList.Font.Name = Font.Name;
pageList.Font.Size = Font.Size;
pageList.ForeColor = ForeColor;
// Embellish the list when there are no pages to list
if (TotalPages <=0 || CurrentPageIndex == -1)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -