asp.net - 在 RowDataBound 上添加 CSS 类

标签 asp.net

我正在尝试将 CSS 类附加到 RowDataBound 上的行。我正在针对 GridView 使用交替 css 类属性,因此我假设这适用于 RowDataBound。如果您以编程方式将 CSS 类分配给 RowDataBound 事件中行的 CssClass 属性,则不会应用交替行样式 CSS 类,即使您附加 CSS 类

这是我得到的:

Protected Sub gvGeneral_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGeneral.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
      If previousExpenseType <> e.Row.Cells(2).Text And previousExpenseType.Length > 0 Then
        e.Row.CssClass += "bottom-border"
      End If

      previousExpenseType = e.Row.Cells(2).Text
    End If
  End Sub

previousExpenseType只是一个我正在进行比较的字符串。如果费用类型发生变化,那么我希望将边框应用于 <tr> 的底部元素。

有什么想法可以解决这个问题吗?似乎在 RowDataBound 之后有一个事件将交替行样式 css 类应用到该行。

最佳答案

在 RowDataBound 事件处理程序中尝试此操作...

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridView grid = GridView1;
    GridViewRow row = e.Row;
    if (row.RowType == DataControlRowType.DataRow)
    {
        bool isAlternating = row.RowState == DataControlRowState.Alternate;

        List<string> classes = new List<string>();

        /*  Setting the CssClass of a row overwrites any CssClass declared in the 
         *  markup, so lets save the value that is in the markup and add to it.
         */
        if (isAlternating) { classes.Add(grid.AlternatingRowStyle.CssClass); }
        else { classes.Add(grid.RowStyle.CssClass); }

        //
        //logic for adding other css classes to the row...
        //

        //set the CssClass property of the row to the combined css classes value
        row.CssClass = string.Join(" ", classes.ToArray());

        //
        //more row processing...
        //
    }
}

关于asp.net - 在 RowDataBound 上添加 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/915248/

相关文章:

asp.net - 在 div 中显示图像

c# - 如何使用 LINQ 在函数中定义返回类型?

c# - Entity Framework 启用迁移

javascript - 找不到动态 asp :Control inside a Gridview using JQuery

asp.net - Facebook 游戏开发 - .NET - 哪种架构?

c# - 如何从 Session 中删除一个值?

asp.net - HttpContext 为空?

html - 如何防止 ASP.NET 在禁用的复选框周围使用 aspNetDisabled 呈现额外的跨度?

c# - 转发到 Controller 的动态 DateTime 控件数量

javascript - 为什么我的值(value)四舍五入(向上或向下)