DataGrid分页中文效果,与大家共享,顺便有能者把DropDownList的触发找到。我是放在呈现上。
///<copyright>kkding 2005</copyright>
///<version>V1.0 </verion>
///<createdate>2003-7-7</createdate>
///<author>丁科康</author>
///<email>kkdingsoft@sina.com</email>
///<log date="2003-7-7">创建</log>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Reflection;
using System.Globalization;
namespace Kkding.Web.UI.WebControls
{
#region Summary
/// <summary>
/// DataGrid 的摘要说明。
/// </summary>
#endregion
[DefaultProperty("Text"), ToolboxBitmap(typeof(System.Web.UI.WebControls.DataGrid)),
ToolboxData("<{0}:DataGrid runat=server></{0}:DataGrid>")]
public class DataGrid : System.Web.UI.WebControls.DataGrid,INamingContainer {
#region Variable
private string sStartPage="首页";
private string sPrevPage="上页";
private string sNextPage="下页";
private string sEndPage="尾页";
//private int maxRecord=0;
private string pageCSS="scrollButton";
private const string PAGERBAR="共<font color=red>$MaxRecord$</font>条/每页<font color=red>$PageSize$</font>条 当前第<font color=red>$CurrentPage$</font>页/共<font color=red>$PageCount$</font>页";
private DropDownList ddlbottum;
private DropDownList ddltop;
private bool bottumflag =false;
#endregion
#region Event
//public event DataGridPageChangedEventHandler PageIndexChanged;
#endregion
#region Property
/// <summary>
/// 总记录数
/// </summary>
[Bindable(true),
Category("分页"),
Description("总记录数")]
public int MaxRecord{
get{
object obj=ViewState["maxRecord"];
return ((null ==obj ) ? 0 : Convert.ToInt32( obj));
}
set{
VirtualItemCount = (value-1) / PageSize+1;
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;
}
}
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
[BrowsableAttribute(false)]
[DescriptionAttribute("DataGrid_PageCount")]
public new int PageCount
{
get
{
if (PagerStyle.Mode == PagerMode.NextPrev)
{
return VirtualItemCount;
}
else
{
return base.PageCount;
}
}
}
[BrowsableAttribute(false)]
[BindableAttribute(true)]
[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
[DescriptionAttribute("DataGrid_CurrentPageIndex")]
public int CurrentPage
{
get
{
object local = base.ViewState["CurrentPage"];
if (local != null)
{
return (int)local;
}
else
{
return 0;
}
}
set
{
if (value < 0)
{
throw new ArgumentOutOfRangeException("value");
}
base.ViewState["CurrentPage"] = value;
}
}
#endregion
#region 构造
/*
public DataGridTable():base(){
}*/
#endregion
#region Method
protected override void OnInit(EventArgs e){
EnableViewState = true;
//CssClass="coolGrid";
base.OnInit(e);
//this.Attributes["cols"] = this.Columns.Count+"";
}
protected override void OnPreRender(EventArgs e) {
OnDropDownListEvent(e,ddltop);
OnDropDownListEvent(e , ddlbottum);
Page.GetPostBackEventReference(this);
}
private void OnDropDownListEvent(EventArgs e , DropDownList ddl)
{
if(ddl != null)
{
if(Convert.ToInt32( ddl.SelectedValue ) !=CurrentPage )
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(e,Convert.ToInt32( ddl.SelectedValue )));
}
}
}
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){
if(source is DataGridItem)
{
if(PagerStyle.Mode == PagerMode.NextPrev)
{
DataGridCommandEventArgs dge = (DataGridCommandEventArgs)e;
if(dge.CommandName.Equals("Page"))
{
OnPageIndexChanged(new DataGridPageChangedEventArgs(dge.Item,Convert.ToInt32( dge.CommandArgument)));
}
return true;
}
}
return base.OnBubbleEvent( source,e);
}
protected override void InitializeItem(DataGridItem item, DataGridColumn[] columns)
{
base.InitializeItem(item,columns);
}
/// <summary>
///
/// </summary>
/// <param name="ddl"></param>
/// <param name="tableCell"></param>
private void InitializeDropDownList(ref DropDownList ddl , TableCell tableCell)
{
ddl = new DropDownList();
ddl.AutoPostBack = true;
for(int i=0;i<PageCount;i++)
{
ddl.Items.Add( new ListItem ( (i+1)+"", i + "" ) );
}
ddl.ClearSelection();
ddl.SelectedIndex = CurrentPage;
tableCell.Controls.Add(new LiteralControl("到第"));
tableCell.Controls.Add( ddl );
tableCell.Controls.Add(new LiteralControl("页"));
}
protected override void InitializePager(DataGridItem item, int columnSpan, PagedDataSource pagedDataSource)
{
TableCell tableCell = new TableCell();
tableCell.ColumnSpan = columnSpan;
DataGridPagerStyle dataGridPagerStyle = PagerStyle;
if (PagerStyle.Mode == PagerMode.NextPrev)
{
tableCell.Controls.Add(new LiteralControl("<table class="+PageCSS+" width=100%> <tr><td nowrap>"));
tableCell.Controls.Add(new LiteralControl( PAGERBAR.Replace("$MaxRecord$",MaxRecord+"").Replace("$PageSize$",PageSize+"").Replace("$CurrentPage$",(CurrentPage+1)+"").Replace("$PageCount$",VirtualItemCount+"")) );
tableCell.Controls.Add(new LiteralControl("</td><td>"));
bool pageflag = (CurrentPage != 0 );
// if (CurrentPage != 0)
// {
LinkButton link1 = new LinkButton();
link1.Text = sStartPage;
link1.CommandName = "Page";
link1.CommandArgument = "0";///"First";
link1.CausesValidation = false;
link1.Enabled = pageflag;
tableCell.Controls.Add(link1);
tableCell.Controls.Add(new LiteralControl(" "));
LinkButton linkButton1 = new LinkButton();
linkButton1.Text = sPrevPage;
linkButton1.CommandName = "Page";
linkButton1.CommandArgument = (CurrentPage-1)+"" ;//"Prev";
linkButton1.CausesValidation = false;
linkButton1.Enabled = pageflag;
tableCell.Controls.Add(linkButton1);
// }
// else
// {
// tableCell.Controls.Add(new LiteralControl(" "));
// }
tableCell.Controls.Add(new LiteralControl(" "));
//if (!pagedDataSource.IsLastPage)
// if(CurrentPage< VirtualItemCount -1)
// {
pageflag = (CurrentPage< VirtualItemCount -1 );
LinkButton linkButton2 = new LinkButton();
linkButton2.Text = sNextPage;
linkButton2.CommandName = "Page";
linkButton2.CommandArgument = (CurrentPage+1)+"" ; //"Next";
linkButton2.CausesValidation = false;
linkButton2.Enabled = pageflag;
tableCell.Controls.Add(linkButton2);
tableCell.Controls.Add(new LiteralControl(" "));
LinkButton link2 = new LinkButton();
link2.Text = sEndPage;
link2.CommandName = "Page";
link2.CommandArgument = (PageCount-1)+""; //"Last";
link2.CausesValidation = false;
link2.Enabled = pageflag;
tableCell.Controls.Add(link2);
// }
// else
// {
// tableCell.Controls.Add(new LiteralControl(" "));
// }
tableCell.Controls.Add(new LiteralControl("</td><td nowrap align='right'>"));
if(!bottumflag)
{
InitializeDropDownList(ref ddlbottum ,tableCell);
}else
{
InitializeDropDownList(ref ddltop ,tableCell);
}
bottumflag=!bottumflag;
tableCell.Controls.Add(new LiteralControl("</td></tr></table>"));
}
else
{
int i1 = pagedDataSource.PageCount;
int j1 = pagedDataSource.CurrentPageIndex + 1;
int k1 = dataGridPagerStyle.PageButtonCount;
int i2 = k1;
if (i1 < i2)
{
i2 = i1;
}
int j2 = 1;
int k2 = i2;
if (j1 > k2)
{
j2 = pagedDataSource.CurrentPageIndex / k1 * k1 + 1;
k2 = j2 + k1 - 1;
if (k2 > i1)
{
k2 = i1;
}
if (k2 - j2 + 1 < k1)
{
j2 = Math.Max(1, k2 - k1 + 1);
}
}
if (j2 != 1)
{
LinkButton linkButton3 = new LinkButton();
linkButton3.Text = "...";
linkButton3.CommandName = "Page";
int k3 = j2 - 1;
linkButton3.CommandArgument = k3.ToString(NumberFormatInfo.InvariantInfo);
linkButton3.CausesValidation = false;
tableCell.Controls.Add(linkButton3);
tableCell.Controls.Add(new LiteralControl(" "));
}
for (int j3 = j2; j3 <= k2; j3++)
{
string str = j3.ToString(NumberFormatInfo.InvariantInfo);
if (j3 == j1)
{
Label label3 = new Label();
label3.Text = str;
tableCell.Controls.Add(label3);
}
else
{
LinkButton linkButton3 = new LinkButton();
linkButton3.Text = str;
linkButton3.CommandName = "Page";
linkButton3.CommandArgument = str;
linkButton3.CausesValidation = false;
tableCell.Controls.Add(linkButton3);
}
if (j3 < k2)
{
tableCell.Controls.Add(new LiteralControl(" "));
}
}
if (i1 > k2)
{
tableCell.Controls.Add(new LiteralControl(" "));
LinkButton linkButton3 = new LinkButton();
linkButton3.Text = "...";
linkButton3.CommandName = "Page";
int k3 = k2 + 1;
linkButton3.CommandArgument = k3.ToString(NumberFormatInfo.InvariantInfo);
linkButton3.CausesValidation = false;
tableCell.Controls.Add(linkButton3);
}
}
item.Cells.Add(tableCell);
}
// /// <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
}
}
from:http://kkding.cnblogs.com/archive/2005/06/29/183197.aspx