asp.net - Gridview 列颜色不填满整个框

标签 asp.net css

我有一个 GridView ,其中某些框以绿色突出显示。这些盒子应该填满整个盒子,但我似乎无法在边缘周围丢弃这个 1px 的边框。我正在使用 IE7,但 FF 也可以。

GridViewSS

呈现的 html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title><link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" /><link href="App_Themes/Contoso/Style.css" type="text/css" rel="stylesheet" /></head>
<body>
    <form name="form1" method="post" action="GridViewColoring.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODIyMTgxMzQxZBgBBQh0ZXN0R3JpZA88KwAMAQgCAWR6Qz5BuXEoalr4HjsTfYqqKPrdwd2ICIXpeNacwdi46w==" />
</div>

    <div>
    <div>
    <table class="cssTable" cellspacing="0" rules="all" border="1" id="testGrid" style="border-collapse:collapse;">
        <tr>
            <th scope="col">Description</th><th scope="col">Serial#</th>
        </tr><tr style="background-color:Yellow;">
            <td class="NoMargin NoPadding" style="font-size:Smaller;">
                        <span id="testGrid_ctl02_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span>
                    </td><td style="font-size:Smaller;">
                        <span id="testGrid_ctl02_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;height:100%;width:100%;">0</span>
                    </td>
        </tr><tr style="background-color:Yellow;">
            <td class="NoMargin NoPadding" style="font-size:Smaller;">
                        <span id="testGrid_ctl03_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span>
                    </td><td style="font-size:Smaller;">
                        <span id="testGrid_ctl03_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;background-color:#CCFFCC;height:100%;width:100%;">1000</span>
                    </td>
        </tr><tr style="background-color:Yellow;">
            <td class="NoMargin NoPadding" style="font-size:Smaller;">
                        <span id="testGrid_ctl04_descriptionLbl">Some desc 1/25/2011 9:51:27 AM</span>
                    </td><td style="font-size:Smaller;">
                        <span id="testGrid_ctl04_serialNumberLbl" class="NoMargin NoPadding MaxHeightAndWidth NoBorder" style="display:inline-block;background-color:#CCFFCC;height:100%;width:100%;">2000</span>
                    </td>
        </tr>
    </table>
</div>
    </div>
    </form>
</body>
</html>

测试用例
CSS

body {
}
.NoMargin
{
    margin:0 0 0 0;
}
.NoPadding
{
    padding:0 0 0 0;
}
.BgColor
{
    background-color:Aqua;
}
.MaxHeightAndWidth
{
    height:100%;
    width:100%;
}
.NoBorder
{
    border:0px;
}

ASP.NET

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewColoring.aspx.cs" Inherits="WebApplication1.GridViewColoring" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView id="testGrid" runat="server" CssClass="cssTable"
            AutoGenerateColumns="False"
            OnRowDataBound="SetStatusColors" >
            <Columns>
                <asp:TemplateField HeaderText="Description" SortExpression="description" ItemStyle-CssClass="NoMargin NoPadding">
                    <ItemTemplate>
                        <asp:Label ID="descriptionLbl" runat="server" Text='<%# Bind("description") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Font-Size="Smaller" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Serial#" SortExpression="serial">
                    <ItemTemplate>
                        <asp:Label ID="serialNumberLbl" runat="server" Text='<%# Bind("serial") %>' CssClass="NoMargin NoPadding MaxHeightAndWidth NoBorder" Height="100%" Width="100%"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Font-Size="Smaller" />
                </asp:TemplateField>                
            </Columns>
            </asp:GridView>
    </div>
    </form>
</body>
</html>

C# 后端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class GridViewColoring : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            testGrid.DataSource = MakeTable();
            testGrid.DataBind();
        }
        protected void SetStatusColors(object sender, GridViewRowEventArgs e)
        {
            for (int i = 0; i < testGrid.Rows.Count; i++)
            {
                string serialNumber = ((Label)testGrid.Rows[i].FindControl("serialNumberLbl")).Text;
                if (serialNumber != "0")
                {
                    //GREEN HIGHLIGHTS
                    ((Label)testGrid.Rows[i].FindControl("serialNumberLbl")).BackColor = System.Drawing.Color.FromArgb(204, 255, 204);
                }
                testGrid.Rows[i].BackColor = System.Drawing.Color.Yellow;
            }
        }
        //mock db
        private DataSet MakeTable()
        {
            var table = new DataTable("ParentTable");
            DataColumn column;
            DataRow row;

            // Create new DataColumn, set DataType, 
            // ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "serial";
            column.ReadOnly = true;

            // Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            //// Create second column.
            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "description";
            column.AutoIncrement = false;
            column.Caption = "Description";
            column.ReadOnly = false;
            column.Unique = false;
            // Add the column to the table.
            table.Columns.Add(column);

            // Instantiate the DataSet variable.
            var dataSet = new DataSet();
            // Add the new DataTable to the DataSet.
            dataSet.Tables.Add(table);

            // Create three new DataRow objects and add 
            // them to the DataTable
            for (int i = 0; i <= 2; i++)
            {
                row = table.NewRow();
                row["serial"] = i * 1000;
                row["description"] = "Some desc " + DateTime.Now;
                table.Rows.Add(row);
            }
            return dataSet;
        }
    }
}

更新
更改了 Serial# 模板的元素样式并解决了该问题。我不知道为什么,但多亏了你的提示,我能够将问题减少到足以尝试的程度:

<ItemStyle Font-Size="Smaller" CssClass="NoMargin NoPadding" />

最佳答案

尝试设置 border-collapse: collapse;在 cssTable CSS 类上。

好的,我能够让它工作。我从第一个模板项中删除了 css 类并创建了以下 css。

table.cssTable
{
    border-collapse: collapse;
}

table.cssTable tr td
{
    background: Yellow;
    font-size:Smaller;
    margin: 0;
    padding: 0;
}

顺便说一下,您应该能够使用此 CSS 摆脱带有字体大小的 ItemStyle。

关于asp.net - Gridview 列颜色不填满整个框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4794910/

相关文章:

html - 找不到如何调整网站元素的大小

Jquery 选择器错误,无法在单击事件中选择元素

html - 为什么这个 HTML 表格对于它的容器来说太宽了?

javascript - 从 javascript 验证特定的验证组

asp.net - IdentityServer4 - 发生安全错误

c# - "Please wait, loading..."文本在 asp.net 页面上的 Page_Load

html - 如何在一张表中显示4列数据。注意: Two xsl:for-each statements are used

具有绝对位置宽度问题的列表中的 Css 列表

asp.net - 我想要用于验证十进制数的正则表达式并且不允许值 0.00

javascript - 如何在浮线图中的x轴上显示时间?