首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RowDataBound上添加CSS类

在RowDataBound上添加CSS类
EN

Stack Overflow用户
提问于 2009-05-27 11:38:03
回答 3查看 16.3K关注 0票数 10

我试图在RowDataBound上的一行中添加一个CSS类。我对GridView使用交替的css类属性,所以我假设这是应用在RowDataBound上。如果在CssClass事件中以编程方式将CSS类分配给行的CssClass属性,那么即使附加了css类,也不会应用交替行样式的CSS类。

我要说的是:

代码语言:javascript
复制
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类应用到该行。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-05-27 11:58:07

您可能希望尝试的另一项工作是在所有行都被数据库化之后执行检查和CSS类操作,方法是使用后面的一个网格视图事件并自己遍历行。我知道这会增加一些额外的处理时间,但取决于所显示的数据集的大小,这是值得的。

票数 2
EN

Stack Overflow用户

发布于 2009-05-27 13:30:07

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

代码语言:javascript
复制
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...
        //
    }
}
票数 13
EN

Stack Overflow用户

发布于 2009-05-27 11:47:31

gridView行数据库只允许您在TR内操作HTML,因此您可能无法选择使用Gridview来做您想做的事情。相反,我建议使用Repeater,它使您能够更好地控制生成的HTML。这里是你可以用中继器做的事情

代码语言:javascript
复制
<table>
    <thead>
        <tr><td>Head1</td><td>Head2</td></tr>
    </thead>
    <asp:Repeater ID="rptTaskItems" runat="server">
        <ItemTemplate>
             <tr><td>column 1 data</td>
                 <td>column 1 data</td>
             </tr>
             <asp:PlaceHolder ID="PHBar" runat="server" visible="false">
             <tr><td colspan="2"><hr/></td>
             </tr>
             </asp:PlaceHolder>
        </ItemTemplate>
    </asp:Repeater>
</table>

在ItemDataBound中,只有在条件为真而仅此而已时,才能使这个占位符PHBar可见。

希望这能有所帮助。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/915248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档