我有一个具有CSS和OnRowDataBound事件的asp:gridview。如果删除OnRowDataBound事件,则CSS会按我的意愿运行,但是如果我离开CSS和OnRowDataBound事件,则CSS永远不会触发。

有人可以帮我同时启动和运行这两种方法吗?

    ASP/HTML
<asp:GridView ID="gvEmps" runat="server" CssClass="Grid" OnRowDataBound="gvEmps_OnRowDataBound" >
    <Columns>
        <asp:BoundField DataField="EmpName" HeaderText="Name"> <ItemStyle CssClass="cellOneCellPadding" Width="50%" /></asp:BoundField>
        <asp:BoundField DataField="EmpAddress" HeaderText="Address"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
        <asp:BoundField DataField="EmpCity" HeaderText="City"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
        <asp:BoundField DataField="EmpState" HeaderText="State"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
        <asp:BoundField DataField="EmpZip" HeaderText="Zip"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
        <asp:BoundField DataField="EmpPhone" HeaderText="Phone"> <ItemStyle HorizontalAlign="Center"/></asp:BoundField>
    </Columns>
</asp:GridView>


C# Method gvEmps_OnRowDataBound....
if (e.Row.RowType == DataControlRowType.DataRow)
{
    foreach (TableCell row in e.Row.Cells)
    {
        if (design1.Any(x => x == e.Row.Cells[0].Text.ToUpper())) { row.CssClass = "des1"; }
        else if (design2.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass = "des2"; }
        else if (design3.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass = "des3"; }
        else { row.CssClass = "defaultview"; };
    }
}

CSS
.cellOneCellPadding {
    padding-left: 9pt !important;
}

最佳答案

您将在OnRowDataBound函数中重新分配CssClass属性,并覆盖模板中最初提供的CssClass。更改您的foreach如下所示:

foreach (TableCell row in e.Row.Cells)
{
    if (design1.Any(x => x == e.Row.Cells[0].Text.ToUpper())) { row.CssClass += " des1"; }
    else if (design2.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass += " des2"; }
    else if (design3.Any(x => x == e.Row.Cells[0].Text)) { row.CssClass += " des3"; }
    else { row.CssClass += " defaultview"; };
}

10-04 12:18