using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.ApplicationBlocks.Data;
namespace JDGLWebControls
{
[DefaultProperty("SelectedValue")]
[ToolboxData("<{0}:SuperGridView runat=server></{0}:SuperGridView>")]
public class SuperGridView : GridView
{
#region"声明变量"
private LinkButton lkFirst;
private LinkButton lkPrev;
private LinkButton lkNext;
private LinkButton lkLast;
private const string PAGERINFORMATION = "共 {0} 条 总共 {1} 页 当前 {2} 页 每页 {3} 条";
private const string PAGERINFORMATION2 = "总共 {1} 页 当前 {2} 页 每页 {3} 条";
private const string NOT_RECORD_FOUND = "<div align=center><b style=color:red;><br>没有记录</b><!--Design By genson email:genson_diy#sina.com --></div>";
#endregion
#region"自定义属性"
/// <summary>
/// 记录总数
/// </summary>
[Category("自定义属性")]
[DefaultValue(-1)]
[Description("自定义记录总数")]
[Browsable(true)]
public virtual int RecordCount
{
get
{
return ViewState["RecordCount"] == null ? -1 : Convert.ToInt32(ViewState["RecordCount"]);
}
set
{
ViewState["RecordCount"] = value;
}
}
/// <summary>
/// 当SuperGridView数据为空的时候,显示的文本
/// </summary>
public string EmptyText
{
get
{
return ViewState["EmptyText"] == null ? NOT_RECORD_FOUND : ViewState["EmptyText"].ToString();
}
set
{
ViewState["EmptyText"] = value;
}
}
#endregion
#region"重写的方法"
protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
InitializeDropDownList(row,columnSpan, pagedDataSource);
CreatePager(row.Cells[0].Controls);
GetRecordCount();
CreatePagerInformation(row.Cells[0].Controls);
//base.InitializePager(row, columnSpan, pagedDataSource);
}
public override object DataSource
{
get
{
return base.DataSource;
}
set
{
base.DataSource = value;
if (value != null)
{
if (DataSource is DataSet)
RecordCount = ((DataSet)DataSource).Tables[0].Rows.Count;
if (DataSource is ICollection)
RecordCount = ((ICollection)DataSource).Count;
if (DataSource is DataTable)
RecordCount = ((DataTable)DataSource).Rows.Count;
if (DataSource is DataView)
RecordCount = ((DataView)DataSource).Table.Rows.Count;
}
}
}
protected override void Render(HtmlTextWriter writer)
{
if (HttpContext.Current != null)
{
if (Rows.Count == 0)
writer.Write(NOT_RECORD_FOUND);
else
base.Render(writer);
}
else
{
base.Render(writer);
}
}
#endregion
#region"自定义方法"
#region"初始化分页用到的DropDownList"
void InitializeDropDownList(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
DropDownList drp = new DropDownList();
drp.AutoPostBack = true;
drp.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
LiteralControl lic = new LiteralControl("转到");
for(int i=0;i<pagedDataSource.PageCount;i++)
{
ListItem li = new ListItem((i+1).ToString() + "页", i.ToString());
if (PageIndex == i)
li.Selected = true;
drp.Items.Add(li);
}
TableCell cell = new TableCell();
cell.Controls.Add(lic);
cell.Controls.Add(drp);
cell.ColumnSpan = columnSpan;
row.Cells.Add(cell);
}
#endregion
#endregion
#region"创建分页控件"
//创建分页控件
public void CreatePager(ControlCollection c)
{
c.Add(new LiteralControl(" "));
lkFirst = new LinkButton();
lkFirst.ID = "lkFirst";
lkFirst.CommandName = "Page";
lkFirst.CommandArgument = "First";
lkFirst.Enabled = true;
lkFirst.Font.Name = "webdings";
lkFirst.Font.Size = FontUnit.Small;
lkFirst.ForeColor = this.ForeColor;
lkFirst.ToolTip = "转到第一页";
lkFirst.Text = "7";
c.Add(lkFirst);
c.Add(new LiteralControl(" "));
lkPrev = new LinkButton();
lkPrev.ID = "lkPrev";
lkPrev.CommandName = "Page";
lkPrev.CommandArgument = "Prev";
lkPrev.Font.Name = "webdings";
lkPrev.Font.Size = FontUnit.Small;
lkPrev.ForeColor = this.ForeColor;
lkPrev.ToolTip = "转到前一页";
lkPrev.Text = "3";
lkPrev.Enabled = true;
if (this.PageIndex == 0) lkPrev.Enabled = false;
c.Add(lkPrev);
c.Add(new LiteralControl(" "));
lkNext = new LinkButton();
lkNext.ID = "lkNext";
lkNext.CommandName = "Page";
lkNext.CommandArgument = "Next";
lkNext.Font.Name = "webdings";
lkNext.Font.Size = FontUnit.Small;
lkNext.ForeColor = this.ForeColor;
lkNext.ToolTip = "转到下一页";
lkNext.Text = "4";
lkNext.Enabled = true;
if (PageIndex == PageCount - 1) lkNext.Enabled = false;
c.Add(lkNext);
c.Add(new LiteralControl(" "));
lkLast = new LinkButton();
lkLast.ID = "lkLast";
lkLast.CommandName = "Page";
lkLast.CommandArgument = "Last";
lkLast.Font.Name = "webdings";
lkLast.Font.Size = FontUnit.Small;
lkLast.ForeColor = this.ForeColor;
lkLast.ToolTip = "转到最后一页";
lkLast.Text = "8";
c.Add(lkLast);
c.Add(new LiteralControl(" "));
}
#endregion
#region"跳页"
public void GoToPage(int pageIndex)
{
GridViewPageEventArgs e = new GridViewPageEventArgs(pageIndex);
this.PageIndex = pageIndex;
OnPageIndexChanging(e);
}
private void drp_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList drpPager = (DropDownList)sender;
GoToPage(int.Parse(drpPager.SelectedValue));
}
#endregion
#region"创建分页信息"
//格式:共 13 条 总共 1 页 当前 1 页
public void CreatePagerInformation(ControlCollection c)
{
LiteralControl lc0 = new LiteralControl(" ");
c.Add(lc0);
LiteralControl lc = new LiteralControl();
if (RecordCount != -1)
{
lc.Text = string.Format(PAGERINFORMATION, RecordCount, PageCount, PageIndex + 1, PageSize);
}
else
{
lc.Text = string.Format(PAGERINFORMATION2, null, PageCount, PageIndex + 1, PageSize);
}
c.Add(lc);
}
#endregion
#region"反射"
private void GetRecordCount()
{
if (!AllowPaging) return;
if (!string.IsNullOrEmpty(DataSourceID))
{
try
{
Control c = this.NamingContainer;
Control datasource= c.FindControl(DataSourceID);
if (datasource is DataSourceControl)
{
DataSourceControl dsc = datasource as DataSourceControl;
if (dsc is SqlDataSource)
{
SqlDataSource sds = dsc as SqlDataSource;
CommandType type = CommandType.StoredProcedure;
if (sds.SelectCommandType == SqlDataSourceCommandType.Text)
{
type = CommandType.Text;
}
DataTable dt = SqlHelper.ExecuteDataset(sds.ConnectionString, type, sds.SelectCommand).Tables[0];
RecordCount = dt.Rows.Count;
}
if (dsc is ObjectDataSource)
{
//ObjectDataSource ods = dsc as ObjectDataSource;
//if (ods.EnablePaging)
//{
// throw new NotSupportedException("Super不支持ObjectDataSource属性EnablePaging=true的分页");
//}
//else
//{
// Type typebll= Type.GetType(ods.TypeName);
// typebll.InvokeMember(ods.SelectMethod, BindingFlags.DeclaredOnly | BindingFlags.Static
// | BindingFlags.InvokeMethod|BindingFlags.Public, null, null, new Object[] { string.Empty });
// // MethodInfo method = typebll.GetMethod(ods.SelectMethod);
//}
}
}
}
catch (Exception ex)
{
//throw ex;
}
}
}
#endregion
}
}