ASP.NET GridView SortedAscendingHeaderStyle不起作用

前端之家收集整理的这篇文章主要介绍了ASP.NET GridView SortedAscendingHeaderStyle不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的SortedAscendingHeaderStyle和SortedDescendingHeaderStyle根本不起作用
<asp:GridView ID="grdProducts" runat="server" CssClass="grid" AllowPaging="True" AllowSorting="True" PageSize="100" EmptyDataText="No data to show"
              onrowdatabound="grdProducts_RowDataBound"  onrowediting="grdProducts_RowEditing" onsorting="grdProducts_Sorting" AutoGenerateEditButton="True">
  <AlternatingRowStyle CssClass="even" />
  <SortedAscendingHeaderStyle ForeColor="White" CssClass="sorted" />
  <SortedDescendingHeaderStyle CssClass="sorted desc" />
</asp:GridView>

单击标题时行正确排序,但是当我使用FireBug检查标题时,它只显示:(这是按升序排序时)

<th scope="col">
  <a href="javascript:__doPostBack('ctl00$body$ctl00$grdProducts','Sort$Namekey')">Namekey</a>
</th>

ForeColor和CssClass根本没有设置.

任何人都知道我做错了什么?

编辑:我的C#代码背后

protected void grdProducts_Sorting(object sender,GridViewSortEventArgs e)
  {
    if ((string)ViewState["SortColumn"] == e.SortExpression)
      ViewState["SortDirection"] = ((string)ViewState["SortDirection"] == "") ? " DESC" : "";
    else
    {
      ViewState["SortColumn"] = e.SortExpression;
      ViewState["SortDirection"] = "";
    }
  }

  protected override void OnPreRender(EventArgs e)
  {
    BindGrid();
    base.OnPreRender(e);
  }

  private void BindGrid()
  {
    string query = "SELECT ... ORDER BY " + ViewState["SortColumn"] + ViewState["SortDirection"];

    DataTable dt = sqlFunctions.Select(query);
    grdProducts.DataSource = dt;
    grdProducts.DataBind();
  }

解决方法

如果您没有使用asp:sqlDataSource作为GridView数据源,我不确定SortedDescendingHeaderStyle是否在没有代码的情况下工作.但是一点编码可以帮助你.

您需要手动将CSS样式应用于标题单元格.您可以在Sorting事件中执行此操作.

protected void grdProducts_Sorting(object sender,GridViewSortEventArgs e)
{
   if ((string)ViewState["SortColumn"] == e.SortExpression)
   {
      ViewState["SortDirection"] = ((string)ViewState["SortDirection"] == "") ? " DESC" : "";
      grdProducts.HeaderRow.Cells[GetColumnIndex( e.SortExpression )].CssClass = "AscendingHeaderStyle";
   }
   else
   {
      ViewState["SortColumn"] = e.SortExpression;
      ViewState["SortDirection"] = "";
      grdProducts.HeaderRow.Cells[GetColumnIndex( e.SortExpression )].CssClass = "DescendingHeaderStyle";
   }
   BindGrid();
}




private int GetColumnIndex( string SortExpression )
{
    int i = 0;
    foreach( DataControlField c in gvwCustomers.Columns )
    {
        if( c.SortExpression == SortExpression )
            break;
        i++;
    }
    return i;
}
原文链接:https://www.f2er.com/aspnet/248078.html

猜你在找的asp.Net相关文章