Adding second class to gridview row in RowDataBound

淺唱寂寞╮ 提交于 2019-12-12 11:59:18

问题


I wish to add an additional class to a GridView programatically. I know I can do this using the following code:

public void RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRow row = ((DataRowView)e.Row.DataItem).Row;
    if (!row.Field<Boolean>("IsActive"))
    {
        e.Row.Attributes["class"] += "InActive";
    }
}

and it works fine. The class "IsActive" is added, however, on alternating rows I end up with this HTML:

<tr class="gvAlternatingStyle" class="InActive"
    onmouseover="gvMouseOver(this)" 
    onmouseout="gvMouseOut(this)" style="cursor:pointer;">

Two class definitions is not what I want. I would prefer to have something like this:

<tr class="gvAlternatingStyle InActive"
    onmouseover="gvMouseOver(this)" 
    onmouseout="gvMouseOut(this)" style="cursor:pointer;">

which is, of course, more valid.

I cannot seem to figure out where/how to adjust this html. Possibly in OnPreRender() but I don't see where. Can anyone give me a pointer?


回答1:


You could take care of the AlternatingRowStyle-CssClass yourself and add the extra class when needed. You will need to remove it from the GridView header of course.

string AlternatingRowStyleCssClass;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string myClass = string.Empty;

        //get the AlternatingRowStyle-CssClass for reference into a variable and delete from the gridview itself
        if (e.Row.RowIndex == 0)
        {
            AlternatingRowStyleCssClass = GridView1.AlternatingRowStyle.CssClass;
            GridView1.AlternatingRowStyle.CssClass = "";
        }

        //check if the row is alternate, if so set the alternating class
        if (e.Row.RowIndex % 2 == 1)
        {
            myClass = AlternatingRowStyleCssClass;
        }

        //check if you need to add the extra class
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        if (!row.Field<Boolean>("IsActive"))
        {
            myClass += " Inactive";
        }

        //add all the classes to the row
        e.Row.Attributes["class"] = myClass.Trim();
    }

    //add the class to the gridview again (maybe relevant for postback)
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        GridView1.AlternatingRowStyle.CssClass = AlternatingRowStyleCssClass;
    }
}



回答2:


After muddling with this a while and with help from VDWWD I worked out how to accomplish this with a combination of the above and OnPreRender():

    public void RowDataBound(object sender, GridViewRowEventArgs e)
    {
            DataRow row = ((DataRowView)e.Row.DataItem).Row;
            if (!row.Field<Boolean>("IsActive"))               {
                e.Row.Attributes["class"] += "InActive";                
    }


    protected void PreRender(object sender, EventArgs e)
    {
        foreach(GridViewRow row in GridView1.Rows)
        {
            if ((row.Attributes["class"] == "InActive")&& 
                (row.RowState == DataControlRowState.Alternate)){
                row.RowState = DataControlRowState.Normal;
                row.Attributes["class"] = "gvAlternatingStyle InActive";

            }

        }
    }


来源:https://stackoverflow.com/questions/42393601/adding-second-class-to-gridview-row-in-rowdatabound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!