简体   繁体   中英

Asp.Net GridView Remove Html Styling

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...

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.

You can simply use skin file (themes) eg:

<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" />
...

Dears, to remove the automatic render of cellpadding="0" , that it is not Html5 compatible, I set the attribute cellspacing to -1 in the markup of GridView or set by code.

<asp:GridView runat="server" CellSpacing="-1"/>
Me.GridView.cellspacing = -1

PS: I use .NET 4.5.

这对我有用:

GridView1.GridLines = GridLines.None;

Adding:

CellSpacing="-1" GridLines="None"

Will clean the markup considerably generating this in the web page:

<table border="0" id="GridView1">

There currently is no way to get rid of the final Border="0" but the above technique does clean up the table considerably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM