Provides custom server control Gridviewcontrol to provide custom display when data is empty, depending on your project needs.
[Defaultproperty ("Enableemptycontentrender")]
[ToolBoxData ("<{0}:gridviewcontrol runat=server></{0}:gridviewcontrol>")]
public class GridViewControl:System.Web.UI.WebControls.GridView
{
///<summary>
///Display the header row if the data is empty
///</summary>
private bool _enableemptycontentrender = true;
///<summary>
///Display the header row if the data is empty
///</summary>
[Bindable (true)]
[Category ("appearance")]
[DefaultValue ("")]
[Localizable (true)]
public bool Enableemptycontentrender
{
set {_enableemptycontentrender = value;}
get {return _enableemptycontentrender;}
}
///<summary>
///Display the header row if the data is empty
///</summary>
private String _emptydatacellcssclass;
///<summary>
///is a space-time information cell style class
///</summary>
[Bindable (true)]
[Category ("appearance")]
[DefaultValue ("")]
[Localizable (true)]
public string Emptydatacellcssclass
{
set {_emptydatacellcssclass = value;}
get {return _emptydatacellcssclass;}
}
///<summary>
///is an empty-time output
///</summary>
///<param name= "writer" ></param>
protected virtual void Renderemptycontent (HtmlTextWriter writer)
{
//create Header Table
Table Tblheader = new Table ();
//copy All Property
Tblheader.cssclass = this. CssClass;
Tblheader.gridlines = this. gridlines;
Tblheader.borderstyle = this. BorderStyle;
Tblheader.borderwidth = this. BorderWidth;
tblheader.cellpadding = this. cellpadding;
tblheader.cellspacing = this. cellspacing;
tblheader.horizontalalign = this. HorizontalAlign;
Tblheader.width = this. Width;
Tblheader.copybaseattributes (this);
TableRow rowheader = new TableRow ();
TblHeader.Rows.Add (Rowheader);
Rowheader.cssclass = "TableHeader";
//generate Table Header
foreach (DataControlField f in this.) Columns)
{
TableCell cell = new TableCell ();
cell. Text = F.headertext;
rowHeader.Cells.Add (cell);
}
TableRow rowbody = new TableRow ();
TblHeader.Rows.Add (rowbody);
TableCell cellbody = new TableCell ();
cellbody.cssclass = This._emptydatacellcssclass;
if (this. EmptyDataTemplate!= null)
//the second row, use the template
this. Emptydatatemplate.instantiatein (Cellbody);
Else
//the second row, use the Emptydatatext
Cellbody.text = this. Emptydatatext;
cellbody.horizontalalign = Horizontalalign.left;
Cellbody.columnspan = this. Columns.count;
RowBody.Cells.Add (cellbody);
Tblheader.rendercontrol (writer);
}
protected override void Render (HtmlTextWriter writer)
{
if (_enableemptycontentrender && (this. Rows.Count = 0 | | This. Rows[0]. RowType = = Datacontrolrowtype.emptydatarow))
Renderemptycontent (writer);
Else
base. Render (writer);
}
}
This article is from the "Learning Achievement Dream" blog, please be sure to keep this source http://qijinchao.blog.51cto.com/1140455/263635