Friday, September 4, 2009

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: }

No comments: