我正在尝试将 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/