namespace DotNet.Controls
{
/// <summary>
/// 分页控件(使用代码实现,不用存储过程)
/// UcPageControlByCode
/// 修改纪录
///
/// 2010-01-06 胡勇 修改转到某页由原来的KeyPress方法改为KeyDown,让用户按回车键确认转页,以防止连续绑定两次。
/// 2011-01-06 胡勇 增加对分页控件的初始化代码:public DataTable InitializePageControl()。
/// 2011-01-05 胡勇 创建分页控件
/// 2011-04-02 胡勇 优化代码、减少不必要的私有变量,去掉多余的代码
///
/// <author>
/// <name>胡勇</name>
/// <QQ>80368704</QQ>
/// <Email>
[email protected]</Email>
/// </author>
/// </summary>
[ToolboxItem(true)]
[DefaultEvent("OnEventPageClicked")]
[ToolboxBitmap(typeof(UcPageControlByCode), "Images.UcPageControlByCodeIcon.png")]
[Description("分页控件(使用代码实现,不用存储过程)")]
public partial class UcPageControlByCode : UserControl
{
#region 私有变量
int recordCount = 0; //记录数
int pageCount = 0; //总页数
int pageIndex = 0; //当前页
#endregion
#region 自定义事件
/// <summary>
/// 单击分页按钮(第一页、上一页、下一页、最后页、跳页)时发生
/// </summary>
[Category("UcPageControlByCode"), Description("单击分页按钮时发生")]
public event EventHandler OnEventPageClicked;
#endregion
#region 自定义属性
private int _pageSize = 50; //分页大小
private string _sqlWhereStatement = string.Empty; //MsSql Where语句
private string _sqlConnString = string.Empty; //MsSql 数据库连接字符串
private string _tableName = string.Empty; //表名
private string _orderField = string.Empty; //数据表的排序字段
private string _primaryKey = string.Empty; //数据表的主键
private string _queryFieldList = "*"; //字段列表(默认为:*)
private DataTable _pageTable = new DataTable();
/// <summary>
/// 返回当前页码
/// </summary>
public int PageIndex
{
get
{
return pageIndex + 1;
}
}
/// <summary>
/// 得到或设置分页大小(默认为:50)
/// </summary>
[Browsable(true), Category("UcPageControlByCode"), Description("得到或设置分页大小(默认为:50)")]
public int PageSize
{
get
{
return _pageSize;
}
set
{
_pageSize = value;
}
}
/// <summary>
/// SQL语句的Where表达式
/// </summary>
[Browsable(false), Category("UcPageControlByCode"), Description("得到或设置SQL语句的Where表达式")]
public string SqlWhereStatement
{
get
{
return _sqlWhereStatement;
}
set
{
_sqlWhereStatement = value;
}
}
/// <summary>
/// 得到或设置SqlServer的连接字符串
/// </summary>
[Browsable(false), Category("UcPageControlByCode"), Description("得到或设置SqlServer的连接字符串")]
public string SqlConnString
{
get
{
return _sqlConnString;
}
set
{
_sqlConnString = value;
}
}
/// <summary>
/// 得到用户单击分页按钮后返回的DataTable
/// </summary>
[Browsable(false), Category("UcPageControlByCode"), Description("得到用户单击分页按钮后返回的DataTable")]
public DataTable PageTable
{
get
{
return _pageTable;
}
}
/// <summary>
/// 设置或得到与分页控件绑定的表名或视图名
/// </summary>
[Browsable(true), Category("UcPageControlByCode"), Description("设置或得到与分页控件绑定的表名或视图名")]
public string TableName
{
get
{
return _tableName;
}
set
{
_tableName = value;
}
}
/// <summary>
/// 设置或得到分页控件排序字段
/// </summary>
[Browsable(true), Category("UcPageControlByCode"), Description("设置或得到分页控件排序字段")]
public string OrderField
{
get
{
return _orderField;
}
set
{
_orderField = value;
}
}
/// <summary>
/// 设置或得到分页控件绑定数据表的主键
/// </summary>
[Browsable(true), Category("UcPageControlByCode"), Description("设置或得到分页控件绑定数据表的主键")]
public string PrimaryKey
{
get
{
return _primaryKey;
}
set
{
_primaryKey = value;
}
}
/// <summary>
/// 设置或得到分页控件绑定的字段列表(默认为:*)
/// </summary>
[Browsable(true), Category("UcPageControlByCode"), Description("设置或得到分页控件绑定的字段列表(默认为:*)")]
public string QueryFieldList
{
get
{
return _queryFieldList;
}
set
{
_queryFieldList = value;
}
}
#endregion
#region 构造函数
/// <summary>
/// 分页控件(使用代码实现,不用存储过程)
/// </summary>
public UcPageControlByCode()
{
InitializeComponent();
}
#endregion
#region 分页实现相关代码
#region void SetUcPageControlPars(string connStr, string whereStatement, string tbName, string orderField, string primaryKeyName, string fieldList):给UcPageControlByCode控件传递必需参数
/// <summary>
/// 给UcPageControlByCode控件传递必需参数
/// </summary>
/// <param name="connStr">连接字符串</param>
/// <param name="whereStatement">MsSql Where语句 </param>
/// <param name="tbName">数据表名或视力名</param>
/// <param name="orderField">排序字段</param>
/// <param name="primaryKeyName">主键值</param>
/// <param name="fieldList">字段列表(默认为:*)</param>
public void SetUcPageControlPars(string connStr, string whereStatement, string tbName
, string orderField, string primaryKeyName, string fieldList)
{
if (string.IsNullOrEmpty(connStr.Trim()))
{
DialogHelper.ShowErrorMsg("温馨提示:\n无可用的数据库连接!");
return;
}
else
{
this.SqlConnString = connStr;
}
this.SqlWhereStatement = whereStatement;
this.TableName = tbName;
this.OrderField = orderField;
this.PrimaryKey = primaryKeyName;
if (!string.IsNullOrEmpty(fieldList.Trim()))
{
this.QueryFieldList = fieldList;
}
}
#endregion
#region DataTable InitializePageControl():初始化UcPageControlByCode
/// <summary>
/// 绑定UcPageControlByCode(并返回包含当前页的DataTable)
/// </summary>
/// <returns>DataTable</returns>
public DataTable BindPageControl()
{
recordCount = GetTotalRecordCount(); //获取总记录数
pageCount = recordCount / PageSize - ModPage(); //保存总页数(减去ModPage()函数防止SQL语句执行时溢出查询范围,可以用存储过程分页算法来理解这句)
pageIndex = 0; //保存一个为0的页面索引值到pageIndex
lblPageCount.Text = (recordCount / PageSize + OverPage()).ToString();//显示lblpageCount、lblrecCount的状态
lblRecCount.Text = recordCount.ToString();
if (recordCount <= PageSize)
{
txtGoToPage.Enabled = false;
}
else
{
txtGoToPage.Enabled = true;
}
return TDataBind();
}
#endregion