Problem: The gridview control introduced in Asp.net 2.0 cannot be displayed when its data source is empty (gridview. datasource = NULL ).
Output header.
Solution:
Method 1: Use its emptytemplate to implement it. Write a static table in the template;
Disadvantage: It is troublesome. You need to set each gridview.
Method 2: if the data source is able, a blank row of datatable is always returned when no data exists;
If the data source is a collection class (arraylist, list <t>, etc.), when no data exists, an empty entity is generated and added to the Collection class.
Disadvantage: it is still troublesome.
Method 3:
It is also a method to introduce to you: extend the gridview to implement. inherit gridvie and rewrite the render method. When the data source is empty, perform the following operations:CodeRight:
/// <Summary>
/// Gridview extension control
/// @ Author: jianyi0115@163.com
/// </Summary>
Public class gridview: system. Web. UI. webcontrols. gridview
{
Private bool _ enableemptycontentrender = True ;
// /<Summary>
// /Whether the title line is displayed when the data is empty
// /</Summary>
Public bool enableemptycontentrender
{
Set {_ enableemptycontentrender = Value ;}
Get { Return _ Enableemptycontentrender ;}
}
Private string _ emptydatacellcssclass;
// /<Summary>
// /When it is null, the Information Cell Style Class
// /</Summary>
Public String emptydatacellcssclass
{
Set {_ emptydatacellcssclass = Value ;}
Get { Return _ Emptydatacellcssclass ;}
}
// /<Summary>
// /Output content when it is null
// /</Summary>
// /<Param name = "Writer"> </param>
Protected virtual Void Renderemptycontent (htmltextwriter writer)
{
Table t = New Table (); // create a table
T. cssclass = This . Cssclass; // copy all property
T. gridlines = This . Gridlines;
T. borderstyle = This . Borderstyle;
T. borderwidth = This . Borderwidth;
T. cellpadding = This . Cellpadding;
T. cellspacing = This . Cellspacing;
T. horizontalalign= This. Horizontalalign;
T. Width= This. Width;
T. copybaseattributes (This);
tablerow row = New tablerow ();
T. rows. add (ROW);
Foreach (datacontrolfield FIn This. Columns) // generate table Header
{
Tablecell Cell= NewTablecell ();
Cell. Text=F. headertext;
Cell. cssclass= "Tdheaderstyle1"; // Write the Header style to death
Row. cells. Add (cell );
}
Tablerow row2= NewTablerow ();
T. Rows. Add (row2 );
Tablecell msgcell= NewTablecell ();
Msgcell. cssclass= This. _ Emptydatacellcssclass;
If ( This . Emptydatatemplate ! = Null ) // Second Row, use the template
{
This . Emptydatatemplate. instantiatein (msgcell );
}
Else // The second row, use Emptydatatext
{
Msgcell. Text = This . Emptydatatext;
}
Msgcell. horizontalalign=Horizontalalign. Center;
Msgcell. columnspan= This. Columns. count;
Row2.cells. Add (msgcell );
T. 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 );
}
}
}
}
I love the improvement!