DataGrid代码(二)

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
///

1<summary>   
2/// DataGrid 的摘要说明。   
3/// </summary>

[
ToolboxBitmap(typeof(MY.UI.WebControls.DataGridTable), "MY.UI.WebControls.DataGridTable.bmp"),
///DefaultProperty("Text"),
ToolboxData("<{0}:DataGridTable runat=server>

")]
#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);
}

///

1<summary>   
2/// 捕获回发事件   
3/// </summary>

///

1<param name="eventArgument"/>

命令行
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));
}

///

1<summary>   
2/// 将此控件呈现给指定的输出参数。   
3/// </summary>

///

1<param name="output"/>

要写出到的 HTML 编写器
///

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("  共

1<font color="red">"+MaxRecord+"</font>

条/每页

1<font color="red">"+PageSize+"</font>

条"));
td.Controls.Add(new LiteralControl(" 当前第

1<font color="red">"+(CurrentPageIndex+1)+"</font>

页/共

1<font color="red">"+PageCount+"</font>

页"));
td.Controls.Add(new LiteralControl("

 1<td border-bottom-style:="" border-left-style:="" border-right-style:="" none;="" none\"="" style='\"BORDER-TOP-STYLE:'>"));   
 2if(CurrentPageIndex!=0)   
 3{   
 4startPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"startPage");   
 5prevPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"prevPage");   
 6td.Controls.Add(startPage);   
 7td.Controls.Add(new LiteralControl(" "));   
 8td.Controls.Add(prevPage);   
 9td.Controls.Add(new LiteralControl(" "));   
10}   
11else   
12{   
13td.Controls.Add(new LiteralControl("          "));   
14}   
15if(CurrentPageIndex&lt;(PageCount-1))   
16{   
17nextPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"nextPage");   
18endPage.Attributes["href"]="javascript:"+Page.GetPostBackEventReference(this,"endPage"+(PageCount-1));   
19td.Controls.Add(nextPage);   
20td.Controls.Add(new LiteralControl(" "));   
21td.Controls.Add(endPage);   
22td.Controls.Add(new LiteralControl(" "));   
23}   
24else   
25{   
26td.Controls.Add(new LiteralControl("          "));   
27}   
28for(int i=0;i<pagecount;i++) if(i="CurrentPageIndex)" list="new" list.selected="true;" listitem="" listitem((i+1).tostring());="" literalcontrol("<="" numpage.items.add(list);="" td="" td.controls.add(new="" {="" }=""><td align="right" border-bottom-style:="" border-left-style:="" border-right-style:="" none;="" none\"="" style='\"BORDER-TOP-STYLE:'>转到第"));   
29td.Controls.Add(numPage);   
30td.Controls.Add(new LiteralControl("页"));   
31if(PagerStyle.Position==PagerPosition.Top||PagerStyle.Position==PagerPosition.TopAndBottom)   
32{   
33numPage.ID=this.UniqueID+":numPageTop";   
34numPage.Attributes["onchange"]=Page.GetPostBackEventReference(this,"numPageTop");   
35table.RenderControl(output);   
36}   
37base.Render(output);   
38  
39if(PagerStyle.Position==PagerPosition.Bottom||PagerStyle.Position==PagerPosition.TopAndBottom)   
40{   
41numPage.ID=this.UniqueID+":numPageBottom";   
42numPage.Attributes["onchange"]=Page.GetPostBackEventReference(this,"numPageBottom");   
43table.RenderControl(output);   
44}   
45myPanel.RenderEndTag(output);   
46}   
47else   
48{   
49base.Render(output);   
50}   
51EnableViewState=false;   
52} 
53
54#endregion   
55} 
56
57  
58}</td></pagecount;i++)></td>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus