views:

849

answers:

2

When I'm using a grid view in Asp.Net, its auto-generated this ugly Html style : cellspacing="0" rules="all" border="1" style="border-collapse:collapse;

Is there a way to not have this styling at all ?

UPDATE

Presently what I got is :

<table cellspacing="0" rules="all" border="1" id="ctl00_cphMain_gvTest" style="border-collapse:collapse;">

What I want :

<table id="ctl00_cphMain_gvTest">

So, no Html style at all. I want clean Html, I'll use CSS if I want to add style...

+2  A: 

You can simply use skin file (themes) e.g.:

<asp:GridView runat="server" BorderStyle="None" CellSpacing="5"/>

or you can write ControlAdapter in which you can control the whole rendering of GridView.

public class GridViewAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
{
  protected override void RenderContents(HtmlTextWriter writer)
        {                
                GridView gridView = Control as GridView;
                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);
...

then add adapter in browser file:

<browsers>
  <browser refID="Default">
    <controlAdapters>    
      <adapter controlType="System.Web.UI.WebControls.GridView"
               adapterType="CSSFriendly.GridViewAdapter" />
...
Jan Remunda
+3  A: 

Take a look at the CSS friendly ASP.NET 2.0 Control Adapaters on the ASP.NET website.

It not only strips out most of the ugly attributes but also adds thead and tbody tags.

Chris Pebble