GridView项目未正确对齐

时间:2013-11-11 14:45:52

标签: asp.net css gridview

我有一个asp.net网页,其中包含一个asp.net GridView控件,该控件在运行时填充了数据绑定项。这是我的GridView HeaderTemplate和ItemTemplate的aspx代码:

<asp:GridView ID="ChildGridView" runat="server" AutoGenerateColumns="false" Width="720px"
      GridLines="None" RowStyle-BorderStyle="Solid" RowStyle-BorderColor="Transparent" OnRowDataBound ="ChildGridView_RowDataBound"
      EmptyDataText="No Records!">
      <Columns>
        <asp:TemplateField>
          <HeaderTemplate>
            <table width="100%" style="background-color: #7C7C7C; color: White; font-family: Arial,Helvetica Neue,Helvetica,sans-serif;
              font-size: 14px; line-height: 1.0; text-align: left; border-radius: 4px 4px 4px 4px;">
              <tr>
                <td style="height: 30px; text-align: left; width: 6%" id="tdYMME" runat="server"
                  visible='<%# ReturnVisibility("tdYMME") %>'>
                  YMME
                </td>
                <td id="tdHollander" runat="server" style="height: 30px; text-align: left; width: 6%"
                  visible='<%# ReturnVisibility("tdHollander") %>'>
                  HOLLANDER
                </td>
                <td id="td1" runat="server" style="height: 30px; text-align: left; width: 6%" visible='<%# ReturnVisibility("tdPartNumber") %>'>
                  <asp:Label ID="lblPartNumber" Text="<%$ Resources:Content, PartNumber %>" runat="server"></asp:Label>
                </td>
                <td id="tdStockNumber" runat="server" style="height: 30px; text-align: left; width: 5%"
                  visible='<%# ReturnVisibility("tdStockNumber") %>'>
                  STOCK
                </td>
                <td id="tdYardLocation" runat="server" style="height: 30px; text-align: left; width: 6%"
                  visible='<%# ReturnVisibility("tdYardLocation") %>'>
                  <asp:Label ID="lblLocation" Text="<%$ Resources:Content, Location %>" runat="server"></asp:Label>
                </td>
                <td id="tdPartType" runat="server" style="height: 30px; text-align: left; width: 6%"
                  visible='<%# ReturnVisibility("tdPartType") %>'>
                  <asp:Label ID="lblPartType" Text="<%$ Resources:Content, PartType %>" runat="server"></asp:Label>
                </td>
                <td id="tdCoreProDesc" runat="server" style="height: 30px; text-align: left; width: 6%"
                  visible='<%# ReturnVisibility("tdCoreProDesc") %>'>
                  DESCRIPTIONS
                </td>
                <td id="tdCoreProPrice" runat="server" style="height: 30px; text-align: left; width: 5%;"
                  visible='<%# ReturnVisibility("tdCoreProPrice") %>'>
                  <asp:Label ID="lblValues" Text="<%$ Resources:Content, Value %>" runat="server"></asp:Label>
                </td>
                <td id="tdYear" runat="server" style="height: 30px; text-align: left; width: 5%"
                  visible='<%# ReturnVisibility("tdYear") %>'>
                  <asp:Label ID="lblYear" Text="<%$ Resources:Content, Year %>" runat="server"></asp:Label>
                </td>
                <td id="tdMake" runat="server" style="height: 30px; text-align: left; width: 5%"
                  visible='<%# ReturnVisibility("tdMake") %>'>
                  <asp:Label ID="lblMake" Text="<%$ Resources:Content, Make %>" runat="server"></asp:Label>
                </td>
                <td id="tdModel" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdModel") %>'>
                  <asp:Label ID="lblModel" Text="<%$ Resources:Content, Model %>" runat="server"></asp:Label>
                </td>
                <td id="tdSubModel" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdSubModel") %>'>
                  <asp:Label ID="lblSubModel" Text="<%$ Resources:Content, SubModel %>" runat="server"></asp:Label>
                </td>
                <td id="tdPartType1" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdPartType1") %>'>
                  <asp:Label ID="lblPType" Text="<%$ Resources:Content, PartType %>" runat="server"></asp:Label>
                </td>
                <td id="tdCoreProDesc1" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdCoreProDesc1") %>'>
                  DESCRIPTIONS
                </td>
                <td id="tdCoreProPrice1" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdCoreProPrice1") %>'>
                  <asp:Label ID="lblValue" Text="<%$ Resources:Content, Value %>" runat="server"></asp:Label>
                </td>
                <td id="tdBuyList" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdBuyList") %>'>
                  DESCRIPTION
                </td>
                <td id="tdDayInventory" runat="server" style="height: 30px; width: 4%" visible='<%# ReturnVisibility("tdDayInventory") %>'>
                  <%-- D.INVENTORY--%>
                  <asp:Label ID="lblDInventory" Text="<%$ Resources:Content, DInventory %>" runat="server"></asp:Label>
                </td>
                <td id="tdOnHand" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdOnHand") %>'>
                  <asp:Label ID="lblQty" Text="<%$ Resources:Content, Qty %>" runat="server"></asp:Label>
                </td>
                <td id="tdWholeSale" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdWholeSale") %>'>
                  <asp:Label ID="lblSale" Text="<%$ Resources:Content, Sale %>" runat="server"></asp:Label>
                </td>
                <td id="tdBuyListPrice" runat="server" style="height: 30px; text-align: left; width: 4%"
                  visible='<%# ReturnVisibility("tdBuyListPrice") %>'>
                  <asp:Label ID="lblV" Text="<%$ Resources:Content, Value %>" runat="server"></asp:Label>
                </td>
                <td id="tdSave" runat="server" style="width: 4%; text-align: center" visible='<%# ReturnVisibility("tdSave") %>'>
                  <asp:Label ID="lblSave" Text="<%$ Resources:Content, Save %>" runat="server"></asp:Label>
                </td>
              </tr>
            </table>
          </HeaderTemplate>
          <ItemTemplate>
            <table width="100%" style="font-family: Arial,Helvetica Neue,Helvetica,sans-serif;
              font-size: 14px; line-height: 1.0; background-color: #E6E5E5;
              color: Black; border-color: White; border-style: solid; border-width: 0px; border-radius: 4px 4px 4px 4px;">
              <tr>
                <td id="tdrYMME" runat="server" style="width: 6%;" class="grdRow" visible='<%# ReturnVisibility("tdYMME") %>'>
                  <asp:Label ID="lblYMME" runat="server" Text='<%# Eval("YMME") %>'></asp:Label><asp:HiddenField
                    ID="hdnId" runat="server" Value='<%# Eval("Id") %>' />
                </td>
                <td align="left" id="tdrHollanderNumber" runat="server" class="grdRow" style="width: 6%;"
                  visible='<%# ReturnVisibility("tdHollander") %>'>
                  <asp:Label ID="lblHollanderNumber" runat="server" Text='<%# Eval("HollanderNumber") %>'></asp:Label>
                </td>
                <td id="tdrPartNumber" runat="server" class="grdRow" style="height: 30px;
                  width: 6%" visible='<%# ReturnVisibility("tdPartNumber") %>'>
                  <asp:Label ID="lblPartNumber" runat="server" Text='<%# Eval("PartNumber") %>'></asp:Label>
                </td>
                <td id="tdrStockNumber" runat="server" class="grdRow" style="width: 5%;" visible='<%# ReturnVisibility("tdStockNumber") %>'>
                  <asp:Label ID="lblStockNumber" runat="server" Text='<%# Eval("StockNumber") %>'></asp:Label>
                </td>
                <td id="tdrYardLocation" runat="server" class="grdRow" style="width: 6%;" visible='<%# ReturnVisibility("tdYardLocation") %>'>
                  <asp:Label ID="lblYardLocation" runat="server" Text='<%# Eval("YardLocation") %>'></asp:Label>
                </td>
                <td id="tdrPartTypeDescr" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdPartType") %>'>
                  <asp:Label ID="lblPartTypeDescr" runat="server" Text='<%# Eval("PartTypeDescr") %>'></asp:Label>
                </td>
                <td id="tdrCoreProDescr" runat="server" class="grdRow" style="width: 6%;" visible='<%# ReturnVisibility("tdCoreProDesc") %>'>
                  <asp:Label ID="lblCoreProDescr" runat="server" Text='<%# Eval("CoreProDescr") %>'></asp:Label>
                </td>
                <td id="tdrCoreProPrice" runat="server" class="grdRow" style="width: 5%;" visible='<%# ReturnVisibility("tdCoreProPrice") %>'>
                  <asp:Label ID="lblCoreProPrice" runat="server" Text='<%# Eval("CoreProPrice") %>'></asp:Label>
                  <asp:Label ID="lblPartWeight" runat="server" Visible="false" Text='<%# Eval("PartWeight") %>'></asp:Label>
                </td>
                <td id="tdrVehicleYear" runat="server" class="grdRow" style="width: 5%;" visible='<%# ReturnVisibility("tdYear") %>'>
                  <asp:Label ID="lblVehicleYear" runat="server" Text='<%# Eval("VehicleYear") %>'></asp:Label>
                </td>
                <td id="tdrVehicleMake" runat="server" class="grdRow" style="width: 5%;" visible='<%# ReturnVisibility("tdMake") %>'>
                  <asp:Label ID="lblVehicleMake" runat="server" Text='<%# Eval("VehicleMake") %>'></asp:Label>
                </td>
                <td id="tdrVehicleModel" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdModel") %>'>
                  <asp:Label ID="lblVehicleModel" runat="server" Text='<%# Eval("VehicleModel") %>'></asp:Label>
                </td>
                <td id="tdrVehicleSubModel" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdSubModel") %>'>
                  <asp:Label ID="lblVehicleSubModel" runat="server" Text='<%# Eval("VehicleSubModel") %>'></asp:Label>
                </td>
                <td id="tdrPartTypeDescrnew" runat="server" class="grdRow" style="width: 8%;" visible='<%# ReturnVisibility("tdPartType1") %>'>
                  <asp:Label ID="lblPartTypeDescrnew" runat="server" Text='<%# Eval("PartTypeDescr") %>'></asp:Label>
                </td>
                <td id="tdrCoreProDescrnew" runat="server" class="grdRow" style="width: 10%;" visible='<%# ReturnVisibility("tdCoreProDesc1") %>'>
                  <asp:Label ID="lblCoreProDescrnew" runat="server" Text='<%# Eval("CoreProDescr") %>'></asp:Label>
                </td>
                <td id="tdrCoreProPricenew" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdCoreProPrice1") %>'>
                  <asp:Label ID="lblCoreProPricenew" runat="server" Text='<%# Eval("CoreProPrice") %>'></asp:Label>
                </td>
                <td id="tdrBuylistDescr" runat="server" class="grdRow" style="width: 4%; text-align: left;"
                  visible='<%# ReturnVisibility("tdBuyList") %>'>
                  <asp:Label ID="lblBuylistDescr" runat="server" Text='<%# Eval("BuylistDescr") %>'></asp:Label>
                </td>
                <td id="tdrAverageDaysInInventory" runat="server" class="grdRow" style="width: 4%; text-align: left;"
                  visible='<%# ReturnVisibility("tdDayInventory") %>'>
                  <asp:Label ID="lblAverageDaysInInventory" runat="server" Text='<%# Eval("AverageDaysInInventory") %>'></asp:Label>
                </td>
                <td id="tdrOnHandQty" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdOnHand") %>'>
                  <asp:Label ID="lblOnHandQty" runat="server" Text='<%# Eval("OnHandQty") %>'></asp:Label>
                </td>
                <td id="tdrYardWholesalePrice" runat="server" class="grdRow" style="width: 4%;" visible='<%# ReturnVisibility("tdWholeSale") %>'>
                  <asp:Label ID="lblYardWholesalePrice" runat="server" Text='<%# Eval("YardWholesalePrice") %>'></asp:Label>
                </td>
                <td id="tdrBuylistPrice" runat="server" class="grdRow" style="width: 4%;"
                  visible='<%# ReturnVisibility("tdBuyListPrice") %>'>
                  <asp:Label ID="lblBuylistPrice" runat="server" Text='<%# Eval("BuylistPrice") %>'></asp:Label>
                </td>
                <td id="tdrchkSave" runat="server" style="width: 4%; text-align: center;"
                  visible='<%# ReturnVisibility("tdSave") %>'>
                  <asp:CheckBox ID="chkSave" runat="server" AutoPostBack="true" Checked='<%# Eval("checkSave") %>'
                    OnCheckedChanged="ChkSave_CheckedChanged" />
                </td>
              </tr>
            </table>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>

CSS代码:

.grdRow
{
  background-color: #E6E5E5;
  height: 29px;
  color: #000000;
  font-size: 12px;
  text-align: left;
}

GridView项目未与CSS代码正确对齐:

enter image description here

我已经尝试过多次尝试通过调整CSS和aspx代码来纠正这一点(没有成功设置(为Gridview控件和项目设置垂直对齐,我认为它被css类覆盖,在css中设置最大宽度(总是会运行代码后超过这个),在aspx代码中使用宽度%等等...)我没有很长的web开发历史(主要是桌面开发)所以请原谅任何错误。我哪里错了?

1 个答案:

答案 0 :(得分:1)

您希望将每个字段用于TemplateField(或其他字段),而不是一个TemplateField。

Here is the example.

FYI:整个列的可见性应该是DataSource的责任。换句话说,它不应该是GridView的工作。