一个自定义的可以分页,排序,扩展显示grid控件
网络整理 - 07-26
第一次写,忘记注解,对不住.using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Xml;
namespace Grid
{
public class IDArgs : System.EventArgs
{
private string id;
//Constructor.
//
public IDArgs(string Id)
{
this.id =Id;
}
// The NumRings property returns the number of rings
// that the alarm clock has sounded when the alarm event
// is generated.
//
public string ID
{
get { return this.id ;}
}
}
///
/// Summary description for WebCustomControl1.
///
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1 runat=server>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl, IPostBackEventHandler
{
public string EditUrl
{
get
{
if(this.ViewState["EditUrl"]!=null) return (string)this.ViewState["EditUrl"];
else return "";
}
set
{
this.ViewState["EditUrl"]=value;
}
}
public string ExpandColumn
{
get
{
if(this.ViewState["ExpandColumn"]!=null) return (string)this.ViewState["ExpandColumn"];
else return "";
}
set
{
this.ViewState["ExpandColumn"]=value;
}
}
//set paging
public int IsPaging
{
get
{
if(this.ViewState["IsPaging"]!=null) return (int)this.ViewState["IsPaging"];
else return 0;
}
set
{
this.ViewState["IsPaging"]=value;
}
}
public int PageCount
{
get
{
if(this.ViewState["PageCount"]!=null) return (int)this.ViewState["PageCount"];
else return 0;
}
set
{
this.ViewState["PageCount"]=value;
}
}
public int CurrentPage
{
get
{
if(this.ViewState["CurrentPage"]!=null) return (int)this.ViewState["CurrentPage"];
else return 1;
}
set
{
this.ViewState["CurrentPage"]=value;
}
}
//set sort
public int IsSort
{
get
{
if(this.ViewState["IsSort"]!=null) return (int)this.ViewState["IsSort"];
else return 0;
}
set
{
this.ViewState["IsSort"]=value;
}
}
public int sortColumn
{
get
{
if(this.ViewState["sortColumn"]!=null) return (int)this.ViewState["sortColumn"];
else return -1;
}
set
{
this.ViewState["sortColumn"]=value;
}
}
public string sortColumnName
{
get
{
if(this.ViewState["sortColumnName"]!=null) return (string)this.ViewState["sortColumnName"];
else return "";
}
set
{
this.ViewState["sortColumnName"]=value;
}
}
public string sortOrder
{
get
{
if(this.ViewState["sortOrder"]!=null) return (string)this.ViewState["sortOrder"];
else return "ASC";
}
set
{
this.ViewState["sortOrder"]=value;
}
}
public DataSet source
{
get
{
return (DataSet)this.ViewState["source"];
}
set
{
this.ViewState["source"]=value;
}
}
public event EventHandler Delete;
public event EventHandler Refresh;
public void RaisePostDataChangedEvent()
{
// IPostBackDataHandler 协定的一部分。如果曾经从 LoadPostData 方法返回真
// (表示需要引发更改通知),则被调用。由于
// 始终返回假,则此方法只是一个空操作。
}
// protected void OnChange(EventArgs e)
// {
// Sort(this, e);
// }
public void RaisePostBackEvent(String eventArgument)
{
if (eventArgument.IndexOf("|")>-1)
{
IDArgs ide=new IDArgs(eventArgument.Substring(eventArgument.IndexOf("|")+1));
if (Delete!=null)
{
Delete(this,ide);
}
return;
}
string flag="";
switch(eventArgument)
{
case "first":this.CurrentPage =1;break;
case "prev":this.CurrentPage -- ;break;
case "next":this.CurrentPage ++ ;break;
case "last":this.CurrentPage =this.PageCount;break;
case "":break;
default:
{
DataTable dt=source.Tables[0];
int lastColumn=sortColumn;
for(int i=0;i if (eventArgument==dt.Columns[i].ColumnName)
{
sortColumn=i;
break;
}
if (lastColumn==sortColumn) { if (this.sortOrder=="DESC") sortOrder="ASC"; else sortOrder="DESC";}
else sortOrder="ASC";
sortColumnName=eventArgument;
break;
}
}
// if ( flag=="sort")
// {
// if(this.IsSort ==0)
// {
// return;
// }
// }
if ( Refresh!=null) Refresh(this,EventArgs.Empty );
}
private string WriteScipt()
{
string script=@" ";
string style=@"
";
return script+style;
}
private string writeList()
{
int[] columnWidth=new int[] {20,100,100,100,0};
DataTable dt=source.Tables[0];
string title=(this.IsSort ==0)?"title":"SortTitle";
string list="
";
if (ExpandColumn!="") list+="";
if (this.IsSort==1)
{
string sortstr="";
string sortFlag=(this.sortOrder=="ASC")?"5":"6";
//set header and sort
for(int j=1;j {
if (sortColumn==j)
{
sortstr=""+sortFlag+"";
}
else sortstr="";
if (columnWidth[j]==0)
list+=""+dt.Columns[j].ColumnName+sortstr+"";
else
list+=""+dt.Columns[j].ColumnName+sortstr+"";
}
}
else
{
for(int j=1;j {
if (columnWidth[j]==0)
list+=""+dt.Columns[j].ColumnName+"";
else
list+=""+dt.Columns[j].ColumnName+"";
}
}
list+="L
";
list+="";
list+="";
for(int i=0;i {
list+="";
//expand column
if (ExpandColumn!="")
list+="5 ";
//delete column
list+="r";
//edit column
list+="1";
for(int j=1;j {
list+=""+dt.Rows[i][j].ToString()+"";
}
list+="
";
//expand display
if (ExpandColumn!="")
list+=" desc
--------------------------------------------------------------------------------
"+dt.Rows[i][ExpandColumn].ToString()+"
";
}
list +="
";
if (this.IsPaging ==0)return list;
//set pageing
string paging="";
if ( PageCount<2)
paging="9|3|4|:";
else
{
if (this.CurrentPage==1)
paging="9|3|4|:";
else if (this.CurrentPage ==this.PageCount)
paging="9|3|4|:";
else
paging="9|3|4|:";
}
list +="Total:"+this.PageCount+"Page:"+this.CurrentPage+""+paging;
return list;
}
///
/// Render this control to the output parameter specified.
///
/// The HTML writer to write out to
protected override void Render(HtmlTextWriter output)
{
output.Write(WriteScipt());
output.Write(writeList());
}
}
}