【问题标题】:Set a Zebra DataList Table设置 Zebra DataList 表
【发布时间】:2012-12-18 16:26:48
【问题描述】:

首先,我需要从数据库中绑定一个表,以便每一行都是可编辑的。 我决定选择 DataList,因为它有 <EditItemTemplate> 属性,而中继器没有。

我需要创建一个 Zebra 表,为此我创建了以下 CSS 类:

.row:nth-of-type(odd)
{
background-color:Green;
}

问题是,当我在转发器中使用此类时,它可以正常工作,因为转发器的构建方式可以让您对行和列进行编码。 例如:

<asp:Repeater>
<HeaderTemplate>
    <Table ...>
</HeaderTemplate>
<ItemTemplate>
  <tr Class="row">
    <td> ... </td>
    <td> ... </td>
  </tr>
</ItemTepmlate>
<FooterTemplate></Table></FooterTemplate>

但是,当我以同样的方式构建我的 DataList 时,它看起来很奇怪而且很乱。 所以这就是我的 Datalist 的构建方式:

<asp:DataList ID="dlStages" runat="server" DataKeyField="Priority" OnCancelCommand="dlStages_CancelCommand"
    OnEditCommand="dlStages_EditCommand" 
    OnUpdateCommand="dlStages_UpdateCommand" BorderWidth="2" 
     GridLines="Both" CellPadding="20" CellSpacing="20" Width="99%" 
    onitemdatabound="dlStages_ItemDataBound">

    <HeaderTemplate>

            <th>
                Priority
            </th>
            <th>
                Stage
            </th>
            <th>
                Description
            </th>
            <th>
                Command
            </th>
            <th>
                Expected End Date
            </th>
    </HeaderTemplate>
    <ItemTemplate>

            <td>
                <%# Eval("Priority") %>
            </td>
            <td>
                <%# Eval("StatusName") %>
            </td>
            <td>
                <%# Eval("Comments") %>
            </td>
            <td>
                <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" CommandName="edit"></asp:LinkButton>
            </td>
            <td>
                <%# Eval("ExpectedEndDate", "{0:dd-MM-yyyy}")%>
            </td>

    </ItemTemplate>        
    <EditItemTemplate>

            <td>
                <%# Eval("Priority") %>
            </td>
            <td>
                <%# Eval("StatusName") %>
            </td>
            <td>
                <asp:TextBox ID="txtComments" runat="server" Text='<%# Eval("Comments") %>' Width="800px"
                    Height="48px" TextMode="MultiLine"></asp:TextBox>
            </td>
            <td>
                <asp:LinkButton ID="lbtnUpdate" runat="server" Text="Update" CommandName="update"></asp:LinkButton>
                /
                <asp:LinkButton ID="lbtnCancel" runat="server" Text="Cancel" CommandName="cancel"></asp:LinkButton>
            </td>
            <td>

                <asp:TextBox runat="server" ID="txtDate"></asp:TextBox>
                <asp:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="txtDate"
                    TargetControlID="txtDate" Format="dd-MM-yyyy">
                </asp:CalendarExtender>
            </td>
    </EditItemTemplate>
</asp:DataList>

为了给DataList中的每一行设置css类,我在代码后面写了如下代码:

protected void dlStages_ItemDataBound(object sender, DataListItemEventArgs e)
{
    e.Item.CssClass = "row";
}

问题是它不会为我创建斑马表。而是用灰色绘制第一列(我不知道它是从哪里来的)。

谁能告诉我我的代码有什么问题?就我而言,如何在 DataList 控件中实现 ZEBRA 表?

提前致谢

附: 我的问题可能不清楚,所以如果需要澄清,我会更详细地解释。

【问题讨论】:

标签: asp.net css datalist


【解决方案1】:

查看这个相关问题:Creating table inside Datalist

  1. 您没有在模板中指定&lt;tr&gt; 标签,所以DataList 只是生成孤立的&lt;td&gt; 标签

  2. 您可以使用值为TableRepeatLayout 属性让DataList 为您生成一个开始和结束&lt;table&gt; 标记。

【讨论】:

  • 我知道我没有指定 标签。原因是当我这样做时,它会弄乱整个桌子。现在关于您的第二个答案,我添加了RepeatLayout="table",但它仍然没有达到我想要的效果。
  • 看看我帖子中的最后一张图片,我希望每一行都被涂成绿色!我该如何编码?
【解决方案2】:

我知道这是一个老问题,但您应该设置asp:DataListItemStyleAlternatingItemStyle 属性,以便像ItemStyle="normalRow" AlternatingItemStyle="greenRow" 那样获得您想要的条纹

datalist 标记的开头应如下所示:

<style type="text/css">
.normalRow { background-color: white; }
.greenRow { background-color: green; }
</style>
<asp:DataList ID="dlStages" runat="server" DataKeyField="Priority" OnCancelCommand="dlStages_CancelCommand"
    OnEditCommand="dlStages_EditCommand" 
    OnUpdateCommand="dlStages_UpdateCommand" BorderWidth="2" 
     GridLines="Both" CellPadding="20" CellSpacing="20" Width="99%" 
    ItemStyle="normalRow" AlternatingItemStyle="greenRow"
    onitemdatabound="dlStages_ItemDataBound">

【讨论】:

  • 谢谢你的回复,最后还是决定用js。我认为这是最简单、最直观的解决方案。
【解决方案3】:

您可以在DataList控件的CssClass属性中使用bootstrap类:

CssClass="table table-striped"

然后,覆盖 table-striped:

.table-stripedCSS > tbody > tr:nth-child(odd) > td,
.table-stripedCSS > tbody > tr:nth-child(odd) > th {
    background-color:red;

    /*Choose your own color here*/
}

欲了解更多信息:
Bootstrap table striped: How do I change the stripe background colour?

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签