分享
 
 
 

DataGrid代码(二)

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

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("&nbsp;&nbsp;共<font color=red>"+MaxRecord+"</font>条/每页<font color=red>"+PageSize+"</font>条"));

td.Controls.Add(new LiteralControl("&nbsp;当前第<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("&nbsp;"));

td.Controls.Add(prevPage);

td.Controls.Add(new LiteralControl("&nbsp;"));

}

else

{

td.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

}

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("&nbsp;"));

td.Controls.Add(endPage);

td.Controls.Add(new LiteralControl("&nbsp;"));

}

else

{

td.Controls.Add(new LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));

}

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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有