using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
namespace MY.UI.WebControls
{
#region Summary
/// <summary>
/// DataGrid 的摘要说明。
/// </summary>
[
ToolboxBitmap(typeof(MY.UI.WebControls.DataGridTable), "MY.UI.WebControls.DataGridTable.bmp"),
///DefaultProperty("Text"),
ToolboxData("<{0}:DataGridTable runat=server></{0}:DataGridTable>")]
#endregion
public class DataGridTable : System.Web.UI.WebControls.DataGrid,INamingContainer,IPostBackEventHandler
{
#region Variable
private LinkButton startPage =new LinkButton();
private LinkButton prevPage =new LinkButton();
private LinkButton nextPage =new LinkButton();
private LinkButton endPage =new LinkButton();
private string sStartPage="StartPage";
private string sPrevPage="PrevPage";
private string sNextPage="NextPage";
private string sEndPage="EndPage";
private DropDownList numPage=new DropDownList();
private Panel myPanel=new Panel();
//private int maxRecord=0;
private string pageCSS="scrollButton";
#endregion
#region Event
//public event DataGridPageChangedEventHandler PageIndexChanged;
#endregion
#region Property
[Bindable(true),
Category("分页"),
DefaultValue("True"),Description("自定义分页")]
public bool IsAllowPaging
{
get
{
object obj=ViewState["isAllowPaging"];
return ((null ==obj ) ? true : Convert.ToBoolean( obj));
}
set
{
ViewState["isAllowPaging"]=value;
}
}
[Bindable(true),
Category("分页"),
Description("总记录数")]
public int MaxRecord
{
get
{
object obj=ViewState["maxRecord"];
return ((null ==obj ) ? 0 : Convert.ToInt32( obj));
}
set
{
ViewState["maxRecord"]=value;
}
}
[Bindable(true),
Category("翻页"),
DefaultValue("首页"),Description("回到首页")]
public string StartPage
{
get
{
return sStartPage;
}
set
{
sStartPage=value;
}
}
[Bindable(true),
Category("翻页"),
DefaultValue("尾页"),Description("回到尾页")]
public string EndPage
{
get
{
return sEndPage;
}
set
{
sEndPage=value;
}
}
[Bindable(true),
Category("翻页"),
DefaultValue("前页"),Description("上一页")]
public string PrevPage
{
get
{
return sPrevPage;
}
set
{
sPrevPage=value;
}
}
[Bindable(true),
Category("翻页"),
DefaultValue("后页"),Description("下一页")]
public string NextPage
{
get
{
return sNextPage;
}
set
{
sNextPage=value;
}
}
[Bindable(true),
Category("翻页外观"),
DefaultValue(""),Description("按钮的CSS")]
public string PageCSS
{
get
{
return pageCSS;
}
set
{
pageCSS=value;
}
}
#endregion
#region 构造
/*
public DataGridTable():base(){
}*/
#endregion
#region Method
protected override void OnInit(EventArgs e)
{
EnableViewState = true;
//判断样式是否存在,不存在样式进行样式初始化
if ((base.CssClass == null) || base.CssClass.Trim().Equals(""))
{
CssClass = "tableGrid";
}
if ((base.HeaderStyle.CssClass == null) || base.HeaderStyle.CssClass.Trim().Equals(""))
{
HeaderStyle.CssClass = "gridHeader";
}
if ((base.FooterStyle.CssClass == null) || base.FooterStyle.CssClass.Trim().Equals(""))
{
FooterStyle.CssClass = "gridFooter";
}
if ((base.PagerStyle.CssClass == null) || base.PagerStyle.CssClass.Trim().Equals(""))
{
PagerStyle.CssClass = "gridPager";
}
if ((base.ItemStyle.CssClass == null) || base.ItemStyle.CssClass.Trim().Equals(""))
{
ItemStyle.CssClass = "gridItem";
}
if ((base.AlternatingItemStyle.CssClass == null) || base.AlternatingItemStyle.CssClass.Trim().Equals(""))
{
AlternatingItemStyle.CssClass = "gridAltItem";
}
if ((base.EditItemStyle.CssClass == null) || base.EditItemStyle.CssClass.Trim().Equals(""))
{
EditItemStyle.CssClass = "gridEditItem";
}
if ((base.SelectedItemStyle.CssClass == null) || base.SelectedItemStyle.CssClass.Trim().Equals(""))
{
SelectedItemStyle.CssClass = "gridSelectedItem";
}
//~样式初始化完毕
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
Page.GetPostBackEventReference(this);
}
protected override void OnItemDataBound(DataGridItemEventArgs e)
{
base.OnItemDataBound(e);
}
protected override void OnItemCommand(DataGridCommandEventArgs e)
{
base.OnItemCommand(e);
}
protected override bool OnBubbleEvent(object source,EventArgs e)
{
return base.OnBubbleEvent( source,e);
}
/// <summary>
/// 捕获回发事件
/// </summary>
/// <param name="eventArgument">命令行</param>
public void RaisePostBackEvent(string eventArgument)
{
int NewPageIndex=0;
if(PagerStyle.Position.Equals(PagerPosition.Bottom))
{
CurrentPageIndex=Convert.ToInt32( Page.Request.Params[this.UniqueID+":numPageBottom"])-1;
}
else
{
CurrentPageIndex=Convert.ToInt32( Page.Request.Params[this.UniqueID+":numPageTop"])-1;
}
if("prevPage".Equals( eventArgument))
{
NewPageIndex=CurrentPageIndex-1;
}
else if("nextPage".Equals( eventArgument))
{
NewPageIndex=CurrentPageIndex+1;
}
else if(eventArgument.StartsWith("endPage" ))
{
NewPageIndex=Convert.ToInt32(eventArgument.Substring(7));
}
else if("numPageTop".Equals( eventArgument))
{
NewPageIndex=Convert.ToInt32( Page.Request.Params[this.UniqueID+":numPageTop"])-1;
}
else if("numPageBottom".Equals( eventArgument))
{
NewPageIndex=Convert.ToInt32( Page.Request.Params[this.UniqueID+":numPageBottom"])-1;
}
OnPageIndexChanged(new DataGridPageChangedEventArgs(this,NewPageIndex));
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
///
protected override void Render(HtmlTextWriter output)
{
if(IsAllowPaging)
{
AllowPaging=true;
PagerStyle.Visible=false;
myPanel.CopyBaseAttributes(this);
Style.Remove("POSITION");
Style.Remove("Z-INDEX");
myPanel.RenderBeginTag(output);
Table table=new Table();
table.CopyBaseAttributes(this);
table.BackColor=(PagerStyle.BackColor.Equals(Color.Empty))?this.BackColor:PagerStyle.BackColor;
table.BackImageUrl=BackImageUrl;
table.BorderColor=PagerStyle.BorderColor.IsEmpty?BorderColor:PagerStyle.BorderColor;
table.BorderStyle=PagerStyle.BorderStyle;
table.BorderWidth=Unit.Point(0);//(PagerStyle.BorderWidth.IsEmpty)?BorderWidth:PagerStyle.BorderWidth;
table.HorizontalAlign=this.HorizontalAlign;
table.Width=Width;
table.CellPadding=CellPadding;
table.CellSpacing=CellSpacing;
table.GridLines=GridLines;
table.Font.Size=FontUnit.Point(9);
TableRow tr=new TableRow();
table.Controls.Add(tr);
TableCell td=new TableCell();
startPage.CssClass=pageCSS;
startPage.Text=sStartPage;
prevPage.CssClass=pageCSS;
prevPage.Text=sPrevPage;
nextPage.CssClass=pageCSS;
nextPage.Text=sNextPage;
endPage.CssClass=pageCSS;
endPage.Text=sEndPage;
numPage.CssClass=pageCSS;
tr.Controls.Add(td);
td.Attributes["style"]="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none";
td.Controls.Add(new LiteralControl(" 共<font color=red>"+MaxRecord+"</font>条/每页<font color=red>"+PageSize+"</font>条"));
td.Controls.Add(new LiteralControl(" 当前第<font color=red>"+(CurrentPageIndex+1)+"</font>页/共<font color=red>"+PageCount+"</font>页"));
td.Controls.Add(new LiteralControl("</td><td style=\"BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none\">"));
if(CurrentPageIndex!=0)
{
startPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"startPage");
prevPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"prevPage");
td.Controls.Add(startPage);
td.Controls.Add(new LiteralControl(" "));
td.Controls.Add(prevPage);
td.Controls.Add(new LiteralControl(" "));
}
else
{
td.Controls.Add(new LiteralControl(" "));
}
if(CurrentPageIndex<(PageCount-1))
{
nextPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"nextPage");
endPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"endPage"+(PageCount-1));
td.Controls.Add(nextPage);
td.Controls.Add(new LiteralControl(" "));
td.Controls.Add(endPage);
td.Controls.Add(new LiteralControl(" "));
}
else
{
td.Controls.Add(new LiteralControl(" "));
}
for(int i=0;i<PageCount;i++)
{
ListItem list=new ListItem((i+1).ToString());
if(i==CurrentPageIndex) list.Selected=true;
numPage.Items.Add(list);
}
td.Controls.Add(new LiteralControl("</td><td align=right style=\"BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none\">转到第"));
td.Controls.Add(numPage);
td.Controls.Add(new LiteralControl("页"));
if(PagerStyle.Position==PagerPosition.Top||PagerStyle.Position==PagerPosition.TopAndBottom)
{
numPage.ID=this.UniqueID+":numPageTop";
numPage.Attributes["onchange"]=Page.GetPostBackEventReference(this,"numPageTop");
table.RenderControl(output);
}
base.Render(output);
if(PagerStyle.Position==PagerPosition.Bottom||PagerStyle.Position==PagerPosition.TopAndBottom)
{
numPage.ID=this.UniqueID+":numPageBottom";
numPage.Attributes["onchange"]=Page.GetPostBackEventReference(this,"numPageBottom");
table.RenderControl(output);
}
myPanel.RenderEndTag(output);
}
else
{
base.Render(output);
}
EnableViewState=false;
}
#endregion
}
}