asp.net custom server control Gridviewcontrol

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.