ASP.NET 2.0 Resources

Powered by Blogger

CSS Friendly Toolkit: GridView - Visible property and other problems

New version of CSS Friendly Control Adapter Toolkit finally brought support for GridView control. The CSS Friendly Control Adapter Tookit hooks to ASP.NET processing pipeline in the stage where the control markup is generated. It's purpose it to emit clear CSS stylable html markup. The code generated by the toolkit is quite clean, but currently i'm facing some problems with GridView adapter. This aplies to beta 2.0 version.

The first problem (which is currently discussed at ASP.NET Forums) is with Visible property. This adapter ignores the Visible property and GridView columns are rendered even if set to false. There is a bug fix posted on ASP.NET forum where the WriteRows method of GridViewAdapter class is slightly altered to support this property setting. This fix is working, but was working only to a point for me. It does not correctly support setting Visible property for individual cells. A little bit of digging with Reflector showed how it's handled by GridView's Render method and this fix is included in code sample later.

The second problem i noticed was the lack of support for individual Column styling. For example i wanted to align all number columns to the right. GridView's BoundField and other Column fields have ItemStyle-CssClass property for this purpose. But this property is ignored by CSS Control Adapter and makes it impossible to style individual Columns. I'm also including the fix for this "bug"/"feature".

To fix these two problems replace foreach TableCell loop in WriteRows method of GridViewAdapter class (GridViewAdapter.cs) with the following code...

   1:  private void WriteRows(HtmlTextWriter writer, GridView gridView, GridViewRowCollection rows, string tableSection)
   2:  {
   3:      ...
   4:      foreach (TableCell cell in row.Cells)
   5:      {
   6:          DataControlFieldCell controlCell = cell as DataControlFieldCell;
   7:          if (controlCell != null)
   8:          {
   9:              DataControlField controlField = controlCell.ContainingField;
  10:              if (controlField != null)
  11:              {
  12:                  if (!controlField.Visible)
  13:                  {
  14:                      cell.Visible = false;
  15:                  }
  16:                  // copy item CSS class
  17:                  cell.CssClass = controlField.ItemStyle.CssClass;
  18:              }
  19:          }
  20:          writer.WriteLine();
  21:          cell.RenderControl(writer);
  22:      }
  23:      ...
  24:  }

6 Comments:

  • Hi,

    off the topic but out of curiosity: is the code formatting tool the same as in here Omar On Trail: C# code HTML formatting continued...? where do the line numbers come from?

    By Blogger Zvonko, at 3:28 PM  

  • I like this blog is fantastic, is really good written. Congratulation. Do you want to see something more? Read it...: Costa Rica is a country with a extremely sense of freedom. The landscapes are for much the most green in whole center america.The chances of investement are way to high, the average of Americans, European and people of the entire planet who is buying here is up in the sky !!!
    Great investment opportunity in Costa Rica: condos, costa rica real estate, costa rica property. Visit us for more info at: http://www.jaco-bay.com/

    By Blogger Omar Cruz, at 5:46 AM  

  • This post has been removed by the author.

    By Blogger Midhun, at 1:37 PM  

  • This post has been removed by the author.

    By Blogger Midhun, at 1:39 PM  

  • The key is to use something that may be new to you: control adapters. These are little chunks of logic that you add to your web site to effectively "adapt" an ASP.NET control to render the HTML you prefer. The ASP.NET 2.0 CSS Friendly Control Adapters kit

    By Blogger Midhun, at 1:43 PM  

  • "Attempted to read past the end of the stream. itextSharp"

    I got this error when going to make pdf.

    By Blogger Anil Desai. T M Technologies, at 2:12 PM  

Post a Comment

<< Home

Created dolly