【问题标题】:How to give CSS propery gridview sorting click event?如何给 CSS 属性 gridview 排序点击事件?
【发布时间】:2009-06-22 14:59:47
【问题描述】:

我填满了我的 gridView 。还要进行属性排序。但我需要进行图像排序。单击降序 cssclass="sortdescheader"。但我不能那样做。我该怎么做? © 真正使用了下面的代码。请帮我解决以下代码?

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

【问题讨论】:

    标签: c# .net asp.net gridview


    【解决方案1】:

    您可以扩展 gridview 以允许插入排序箭头。这样你就可以在多个地方使用它而无需复制代码:参考:http://www.4guysfromrolla.com/articles/012308-1.aspx

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

    【讨论】:

    • 我很抱歉,当我进行更改时,我没有在 ide 中加载这个。尝试将其设置为空白字符串或普通的 CSS 类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多