Hi all,
unfortunately this fix doesn't work correctly in case of you decide to show the header for EmptyData situation.
For example I have created a new class inherited from GridView to always display the Header (i found a lot of samples in the web).
In this case the code:
/////////////// EmptyDataTemplate ///////////////////////
if (gridView.Rows.Count == 0) {
//Control[0].Control[0] s/b the EmptyDataTemplate.
if (gridView.HasControls()) {
if (gridView.Controls[0].HasControls()) {
if (gridView.Controls[0].Controls[0] is GridViewRow) {
rows.Clear();
rows.Add(gridView.Controls[0].Controls[0]);
gvrc = new GridViewRowCollection(rows);
WriteRows(writer, gridView, gvrc, "tfoot");
}
}
}
} doesn't refers to EmptyDataRow but to Header so it wrongly renderize two Header.
I have fixed this issue with the following code:
....
/////////////// EmptyDataTemplate ///////////////////////
if (gridView.Rows.Count == 0)
{
if (this.GetEmptyDataRow(gridView) != null)
{
rows.Clear();
rows.Add(this.GetEmptyDataRow(gridView));
gvrc = new GridViewRowCollection(rows);
WriteRows(writer, gridView, gvrc, "tfoot");
}
}
....
protected GridViewRow GetEmptyDataRow(GridView gridview)
{
if (gridview.HasControls())
if (gridview.Controls[0].HasControls())
{
foreach (GridViewRow gvr in gridview.Controls[0].Controls)
{
if (gvr.RowType == DataControlRowType.EmptyDataRow)
return gvr;
}
}
return null;
}
IMHO, I think should be more flexible create single protected method for each piece of RenderContents code to permit us override what we need and no all the method.
Something like this:
protected override void RenderContents(HtmlTextWriter writer)
{
if (Extender.AdapterEnabled)
{
AdvancedGridView gridView = Control as AdvancedGridView;
if (gridView != null)
{
writer.Indent++;
WritePagerSection(writer, PagerPosition.Top);
writer.WriteLine();
writer.WriteBeginTag("table");
writer.WriteAttribute("cellpadding", "0");
writer.WriteAttribute("cellspacing", "0");
writer.WriteAttribute("summary", Control.ToolTip);
if (!String.IsNullOrEmpty(gridView.CssClass))
{
writer.WriteAttribute("class", gridView.CssClass);
}
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
///////////////////// CAPTION /////////////////////////////
this.RenderCaption(gridView);
///////////////////// HEAD /////////////////////////////
this.RenderHeader(gridView);
///////////////////// FOOT /////////////////////////////
this.RenderFooter(gridView);
///////////////////// EMPTYDATA /////////////////////////////
this.RenderEmptyData(gridView);
///////////////////// BODY /////////////////////////////
WriteRows(writer, gridView, gridView.Rows, "tbody");
////////////////////////////////////////////////////////
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("table");
WritePagerSection(writer, PagerPosition.Bottom);
writer.Indent--;
writer.WriteLine();
if (gridView.IsInsertingNewRow)
gridView.IsInsertingNewRow = false;
}
}
else
{
base.RenderContents(writer);
}
}
protected void RenderCaption(GridView gridView)
{
if (String.IsNullOrEmpty(gridView.Caption) == false)
{
writer.WriteLine();
writer.WriteBeginTag("caption");
writer.WriteAttribute("class", "AspNet-GridView-Caption");
writer.Write(HtmlTextWriter.TagRightChar);
writer.WriteEncodedText(gridView.Caption);
writer.WriteEndTag("caption");
}
}
protected void RenderHeader(GridView gridView)
{
ArrayList rows = new ArrayList();
GridViewRowCollection gvrc = null;
rows.Clear();
if (gridView.ShowHeader && gridView.HeaderRow != null)
{
rows.Add(gridView.HeaderRow);
}
gvrc = new GridViewRowCollection(rows);
WriteRows(writer, gridView, gvrc, "thead");
}
protected void RenderFooter(GridView gridView)
{
ArrayList rows = new ArrayList();
GridViewRowCollection gvrc = null;
rows.Clear();
if (gridView.ShowFooter && gridView.FooterRow != null)
{
rows.Add(gridView.FooterRow);
}
gvrc = new GridViewRowCollection(rows);
WriteRows(writer, gridView, gvrc, "tfoot");
}
protected void RenderEmptyData(GridView gridView)
{
ArrayList rows = new ArrayList();
GridViewRowCollection gvrc = null;
if (gridView.Rows.Count == 0)
{
if (this.GetEmptyDataRow(gridView) != null)
{
rows.Clear();
rows.Add(this.GetEmptyDataRow(gridView));
gvrc = new GridViewRowCollection(rows);
WriteRows(writer, gridView, gvrc, "tfoot");
}
}
}What do you think?
Hope to see your comments!
Luca Ritossa