向RowDataBound中的gridview行添加第二个类 [英] Adding second class to gridview row in RowDataBound
问题描述
我希望以编程方式向GridView添加一个附加类.我知道我可以使用以下代码来做到这一点:
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";
}
}
,它工作正常.但是,在交替的行上添加了"IsActive"类,最终得到了以下HTML:
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;">
这当然更有效.
我似乎无法弄清楚在何处/如何调整此html.可能在OnPreRender()中,但是我看不到哪里.谁能给我指点?
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?
推荐答案
对此进行了一段时间的摸索,并在VDWWD的帮助下,我研究了如何结合上述和OnPreRender()来实现这一点:
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";
}
}
}
这篇关于向RowDataBound中的gridview行添加第二个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!