Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将第二个类添加到RowDataBound中的gridview行_C#_Asp.net_Gridview - Fatal编程技术网

C# 将第二个类添加到RowDataBound中的gridview行

C# 将第二个类添加到RowDataBound中的gridview行,c#,asp.net,gridview,C#,Asp.net,Gridview,我希望以编程方式向GridView添加一个附加类。我知道我可以使用以下代码执行此操作: public void RowDataBound(object sender, GridViewRowEventArgs e) { DataRow row = ((DataRowView)e.Row.DataItem).Row; if (!row.Field<Boolean>("IsActive")) { e.Row.Attributes["class"]

我希望以编程方式向GridView添加一个附加类。我知道我可以使用以下代码执行此操作:

public void RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRow row = ((DataRowView)e.Row.DataItem).Row;
    if (!row.Field<Boolean>("IsActive"))
    {
        e.Row.Attributes["class"] += "InActive";
    }
}
public void行数据绑定(对象发送方,GridViewRowEventArgs e)
{
DataRow row=((DataRowView)e.row.DataItem).row;
如果(!行字段(“IsActive”))
{
e、 行属性[“类”]+=“非活动”;
}
}
而且效果很好。但是,在交替的行上添加了类“IsActive”,我最终得到以下HTML:

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

两个类定义不是我想要的。我更喜欢这样的东西:

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

当然,这更有效


我似乎不知道在哪里/如何调整这个html。可能在OnPreRender()中,但我不知道在哪里。有人能给我一个指针吗?

你可以自己处理
交替行样式CssClass
并在需要时添加额外的类。当然,您需要将其从GridView标题中删除

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;
    }
}
string alternatingrowstylecsclass;
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
//检查该行是否为datarow
如果(e.Row.RowType==DataControlRowType.DataRow)
{
string myClass=string.Empty;
//将用于引用的AlternatingRowStyle CssClass获取到变量中,并从gridview本身中删除
如果(e.Row.RowIndex==0)
{
AlternatingRowStyleCssClass=GridView1.AlternatingRowStyle.CssClass;
GridView1.AlternatingRowStyle.CssClass=“”;
}
//检查行是否为替换行,如果是,则设置替换类
如果(e.Row.RowIndex%2==1)
{
myClass=AlternatingRowStyleCssClass;
}
//检查是否需要添加额外的类
DataRow row=((DataRowView)e.row.DataItem).row;
如果(!行字段(“IsActive”))
{
myClass+=“非活动”;
}
//将所有类添加到行中
e、 Row.Attributes[“class”]=myClass.Trim();
}
//再次将类添加到gridview(可能与回发相关)
if(e.Row.RowType==DataControlRowType.Footer)
{
GridView1.AlternatingRowStyle.CssClass=AlternatingRowStyleCssClass;
}
}

经过一段时间的摸索,在VDWWD的帮助下,我找到了如何通过上面和OnPreRender()的组合来实现这一点:

public void行数据绑定(对象发送方,GridViewRowEventArgs e)
{
DataRow row=((DataRowView)e.row.DataItem).row;
如果(!行字段(“IsActive”)){
e、 行属性[“类”]+=“非活动”;
}
受保护的void预呈现(对象发送方、事件参数)
{
foreach(GridView1.Rows中的GridViewRow行)
{
如果((行属性[“类”]=“非活动”)&&
(row.RowState==DataControlRowState.Alternate)){
row.RowState=DataControlRowState.Normal;
行属性[“类”]=“gvAlternatingStyle InActive”;
}
}
}

我实际上刚刚实现了类似的功能(当我键入此问题时,我突然想到)但我仍然有兴趣看看是否有一种方法可以不用手工编程交替样式:)。感谢您的快速响应!!如果没有人能直接解决上述问题,我会将您的答案标记为您的答案。我认为在
AlternatingRowStyle CssClass
中定义的类将在以后添加,然后在
OnRowDataBound
event。我用
e.Row.Attributes.Remove(“类”)做了一个快速测试
然后再次添加新类,但这不起作用,仍然添加了该类。是的,我同意。仍在试验。我在OnPreRender中查看了它,但类也不存在。我想补充一点,使用您的解决方案的最大缺点(如上所述)我的应用程序被高度屏蔽。这会破坏网格的外观,增加额外的麻烦。我找到了一个快速修复方法,您仍然可以在gridview标题本身中定义类。然后,它存储在变量中,并从网格中删除,以便以后不再使用,并破坏rowdatabound事件中添加的类。
    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";

            }

        }
    }