Page view counter

Support for EmptyDataTemplate

Rate It (1)

Last post 09-10-2007 9:59 AM by ciberman. 11 replies.

Sort Posts:

  • Support for EmptyDataTemplate

    01-04-2007, 1:06 PM

    I think I've got a fix for the EmptyDataTemplate issue:

    Add the following to RenderContents in GridViewAdapter.cs right before the ///// BODY //// section

    /////////////// 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");
    			}
    		}
    	}
    }					
    

    Add the following to GetRowClass right before return className.Trim();

    //// EmptyDataTemplate 
    if ((row.RowType & DataControlRowType.EmptyDataRow) == DataControlRowType.EmptyDataRow) {
    	className += " AspNet-GridView-Empty ";
    }
    

    Finally, if you want to override the standard tfoot style, add a CSS section in GridViewExample -

    /* Optional AspNet-GridView-Empty to override tfoot used for EmptyDataTemplate */
    .AspNet-GridView table tfoot tr.AspNet-GridView-Empty td 
    {
    	color: #F7F6F3;
    	background: #5D7B9D;
    	font-weight: bold;
    	border-bottom: solid 1px #CCCCCC;
    	border-right: solid 1px #CCCCCC;
    	padding: 2px;
    }
    

    I've only worked with the GridView so far, hopefully someone can adapt this where necessary.

    -Chris

    http://www.christopherlewis.com
  • Re: Support for EmptyDataTemplate

    03-14-2007, 10:29 AM
    • Loading...
    • bdemarzo
    • Joined on 07-02-2002, 8:05 AM
    • New York
    • Posts 168
    • Points 435
    For those interested, this fix was incorporated into the CodePlex distribution of the CSS adapters. Use changeset 3263 or later.
  • Re: Support for EmptyDataTemplate

    03-28-2007, 12:42 PM
    • Loading...
    • dbhikadiya
    • Joined on 03-16-2007, 10:07 PM
    • Long Island, NY
    • Posts 1
    • Points 2

    This fix injects an issue in existing rendering function. I am seeing it in VB.Net. However, I think it may be same for C#.

    Public Class GridViewAdapter
      Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
      Private Sub WriteRows(ByVal writer As HtmlTextWriter, ByVal gridView As GridView, ByVal rows As GridViewRowCollection, ByVal tableSection As String)
      ...
    
      Dim cell As TableCell
      For Each cell In row.Cells
        Dim fieldCell As DataControlFieldCell = cell
    
      ...
      End Sub
    
    ...
    End Class

     For EmptyDataTemplate, cell is of type TableCell which throws an exception as it can not be casted to DataControlFieldCell.

     Here is the suggested fix:

    Public Class GridViewAdapter
      Inherits System.Web.UI.WebControls.Adapters.WebControlAdapter
      Private Sub WriteRows(ByVal writer As HtmlTextWriter, ByVal gridView As GridView, ByVal rows As GridViewRowCollection, ByVal tableSection As String)
      ...
    
      Dim cell As TableCell
      For Each cell In row.Cells
        If (TypeOf cell Is DataControlFieldCell) Then
          Dim fieldCell As DataControlFieldCell = cell
          ...
        End If
      ...
      End Sub
    
    ...
    End Class
    
     
    Filed under:
  • Re: Support for EmptyDataTemplate

    03-28-2007, 12:54 PM
    • Loading...
    • bdemarzo
    • Joined on 07-02-2002, 8:05 AM
    • New York
    • Posts 168
    • Points 435
    The code version on CodePlex, which is only available in C#, has the following code which should not experience the problem:
    foreach (TableCell cell in row.Cells)
    {
    	DataControlFieldCell fieldCell = cell as DataControlFieldCell;
    	if ((fieldCell != null) && (fieldCell.ContainingField != null))
    	{
    

    The "as" keyword ensures fieldCell is null of not of type DataControlFieldCell, and the following "if" statement checks for the null value.


  • Re: Support for EmptyDataTemplate

    04-12-2007, 7:16 AM
    • Loading...
    • solanky
    • Joined on 09-06-2005, 4:04 AM
    • India
    • Posts 10
    • Points 44

    Thanks Chris for the fix, I have used it and its working well. I have downloaded the current version from CodePlex as suggested but it has not solved the problem.

     

    Deependra Solanky
    http://www.madaboutanything.com
  • Re: Support for EmptyDataTemplate

    04-12-2007, 2:37 PM
    • Loading...
    • bdemarzo
    • Joined on 07-02-2002, 8:05 AM
    • New York
    • Posts 168
    • Points 435

    The current "release" version from CodePlex is the same as the original release from Microsoft. TO get the changes outlined here (and other subsequent changes to the community version since that release) you have to download the latest build from the "Source Code" tab.

     

  • Re: Support for EmptyDataTemplate

    04-17-2007, 11:04 AM
    • Loading...
    • mattchung
    • Joined on 01-18-2007, 1:28 AM
    • Posts 6
    • Points 12

    thank you for your source.

    however, i checked the codeplex css friendly page and found nothing listed under the "source code" tab. am i missing something?

    thank you.

     

  • Re: Support for EmptyDataTemplate

    04-17-2007, 11:46 AM
    • Loading...
    • bdemarzo
    • Joined on 07-02-2002, 8:05 AM
    • New York
    • Posts 168
    • Points 435

    No, you're not missing anything. What happened is CodePlex lost one of their source control servers, which happened to be the one we used, and in turn lost all our source code and work items. (See this thread.) They've since moved us to a new source control server. I will have to check-in the latest code release to the new server. When I do that I'll post a note here (probably will be some time today).

     

  • Re: Support for EmptyDataTemplate

    04-17-2007, 9:35 PM
    • Loading...
    • mattchung
    • Joined on 01-18-2007, 1:28 AM
    • Posts 6
    • Points 12

    wow, what are the chances of that happening!

    i'll just continue to check the site then. thank you!
     

  • Re: Support for EmptyDataTemplate

    04-18-2007, 12:51 AM
    • Loading...
    • bdemarzo
    • Joined on 07-02-2002, 8:05 AM
    • New York
    • Posts 168
    • Points 435

    After fighting for 30 minutes I got the code checked back in: http://www.codeplex.com/cssfriendly/SourceControl/ListDownloadableCommits.aspx

    If CodePlex used Subversion, I'd be much happier (and would have gotten 29 minutes of my life back).

    The latest code check-in includes the emptydatatemplate and a radiobuttonlist adapter.


     

  • Re: Support for EmptyDataTemplate

    07-26-2007, 9:54 AM
    • Loading...
    • ryanrr
    • Joined on 05-22-2007, 8:36 PM
    • Posts 6
    • Points 8

    FYI, one can use the TryCast() method in VB.Net to give the equivalent of the C# "as" keyword.

    Anyways, it's probably best to also include the user-specified EmptyDataRowCssClass for the EmptyDataTemplate for the GetRowClass() function:

                '//// EmptyDataTemplate

                If ((row.RowType And DataControlRowType.EmptyDataRow) = DataControlRowType.EmptyDataRow) Then

                    className &= " AspNet-GridView-Empty "

                    If (Not IsNothing(gridView.EmptyDataRowStyle)) Then

                        className &= gridView.EmptyDataRowStyle.CssClass

                    End If

                End If

  • Re: Support for EmptyDataTemplate

    09-10-2007, 9:59 AM
    • Loading...
    • ciberman
    • Joined on 04-21-2007, 11:48 AM
    • Trieste
    • Posts 1
    • Points 2

    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 
Page 1 of 1 (12 items)