Asp.Net GridView删除Html样式

时间:2023-01-08 21:08:56

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;

当我在Asp.Net中使用网格视图时,它会自动生成这种丑陋的Html样式: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...

所以,根本没有Html风格。我想要干净的Html,如果我想添加样式,我会使用CSS ...

5 个解决方案

#1


8  

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

看一下ASP.NET网站上的CSS友好的ASP.NET 2.0控件适配器。

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

它不仅剥离了大部分丑陋的属性,还添加了thead和tbody标签。

#2


3  

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.

或者您可以编写ControlAdapter,您可以在其中控制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" />
...

#3


1  

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.

Dears,为了删除cellpadding =“0”的自动渲染,它不是Html5兼容,我在GridView的标记中将属性cellspacing设置为-1或由代码设置。

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

PS: I use .NET 4.5.

PS:我使用的是.NET 4.5。

#4


0  

This works for me:

这对我有用:

GridView1.GridLines = GridLines.None;

#5


0  

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.

目前没有办法摆脱最后的Border =“0”,但上述技术确实大大清理了表格。

#1


8  

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

看一下ASP.NET网站上的CSS友好的ASP.NET 2.0控件适配器。

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

它不仅剥离了大部分丑陋的属性,还添加了thead和tbody标签。

#2


3  

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.

或者您可以编写ControlAdapter,您可以在其中控制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" />
...

#3


1  

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.

Dears,为了删除cellpadding =“0”的自动渲染,它不是Html5兼容,我在GridView的标记中将属性cellspacing设置为-1或由代码设置。

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

PS: I use .NET 4.5.

PS:我使用的是.NET 4.5。

#4


0  

This works for me:

这对我有用:

GridView1.GridLines = GridLines.None;

#5


0  

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.

目前没有办法摆脱最后的Border =“0”,但上述技术确实大大清理了表格。