gpt4 book ai didi

c# - 将第二类添加到 RowDataBound 中的 gridview 行

转载 作者:太空狗 更新时间:2023-10-30 01:30:54 24 4
gpt4 key购买 nike

我希望以编程方式向 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";
}
}

而且效果很好。添加了“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() 中,但我看不到在哪里。谁能给我指点一下?

最佳答案

您可以自己处理 AlternatingRowStyle-CssClass 并在需要时添加额外的类。当然,您需要将其从 GridView header 中删除。

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;
}
}

关于c# - 将第二类添加到 RowDataBound 中的 gridview 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com