C# 如何给CSS属性gridview排序点击事件?
我填写了我的表格。同时给出属性排序。但我需要进行上下图像排序。单击降序cssclass=“sortdescheder”。但我不能这样做。我怎么做呢?İ真正用于以下代码。请帮我输入以下代码C# 如何给CSS属性gridview排序点击事件?,c#,.net,asp.net,gridview,C#,.net,Asp.net,Gridview,我填写了我的表格。同时给出属性排序。但我需要进行上下图像排序。单击降序cssclass=“sortdescheder”。但我不能这样做。我怎么做呢?İ真正用于以下代码。请帮我输入以下代码 protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e) { GridView gridView = (GridView)sender; if (gridView.Sort
protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gridView = (GridView)sender;
if (gridView.SortExpression.Length > 0)
{
int cellIndex = -1;
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == gridView.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (cellIndex > -1)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[cellIndex].CssClass += (gridView.SortDirection == SortDirection.Ascending
? " sortascheader" : " sortdescheader");
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[cellIndex].CssClass += (e.Row.RowIndex % 2 == 0 ? " sortaltrow" : "sortrow");
}
}
}
}
可以扩展gridview以允许插入排序箭头。这样,您就可以在多个位置使用它,而无需复制代码:reference:
很抱歉,我在进行更改时没有在ide中加载此内容。尝试将其设置为空白字符串或普通CSS类
public class GridView : System.Web.UI.WebControls.GridView
{
protected override void OnSorted(EventArgs e)
{
string AscCSS = ...;
string DescCSS= ...;
foreach (DataControlField field in this.Columns)
{
// strip off the old ascending/descending icon
field.HeaderStyle.CssClass.Remove();
// See where to add the sort ascending/descending icon
if (field.SortExpression == this.SortExpression)
{
if (this.SortDirection == SortDirection.Ascending)
field.HeaderStyle.CssClass = AscCSS;
else
field.HeaderStyle.CssClass = DescCss;
}
}
base.OnSorted(e);
}
}