ASP.NET GridView SortedAscendingHeaderStyle不起作用

时间:2011-10-07 00:24:22

标签: asp.net gridview

我的SortedAscendingHeaderStyleSortedDescendingHeaderStyle根本无法正常工作

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

4 个答案:

答案 0 :(得分:14)

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

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

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

答案 1 :(得分:6)

我没有足够的代表对接受的答案发表评论。当我尝试应用解决方案时,它会正确排序,但不会将CSS类应用于最终呈现的内容。

在我的例子中,在对我的DataSource(List)进行排序并将其指定为网格的DataSource之后,在我的网格上调用DataBind(),但是在设置CssClass之前就行了。如果其他人遇到类似的事情,我会分享。

答案 2 :(得分:0)

我认为这是数据绑定的时间。将您的数据绑定更改为如下所示:

  protected void Page_Load(object sender, EventArgs e)
  {    
    if (!Page.IsPostBack)
    {
        BindGrid();      
    }
  }

  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"] = "";
    }
    BindGrid();
  }

GridView.Sorting Event

答案 3 :(得分:0)

后期,但仅供参考。使用以下命令对我有用:

这是我的代码(在x.aspx中):

<asp:SqlDataSource ID="SqlDataSourceX" runat="server" ConnectionString="xxx"
    EnableViewState="False" OnSelecting="SqlDataSourceXSelecting"></asp:SqlDataSource>

<asp:GridView ....
    AllowSorting="True"
    EnableSortingAndPagingCallbacks="False"
    OnSorted="GridViewResults_OnSorted" ....           DataSourceID="SqlDataSourceX" CssClass="table table-bordered text-left">
    <SortedAscendingHeaderStyle CssClass="SortedAscendingHeaderStyle"></SortedAscendingHeaderStyle>
    <SortedDescendingHeaderStyle CssClass="SortedDescendingHeaderStyle"></SortedDescendingHeaderStyle>
    <Columns>

...     

这是我的代码(在x.aspx.cs中):

protected void GridViewResults_OnSorted(object sender, EventArgs e) {
   ExecuteSearch(); //Adds some where clauses to the SQL Data Source, no explicit sorting here
}

然后,在单击排序后,将创建以下表标题:

<th class="SortedDescendingHeaderStyle" scope="col">
    <a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridViewResults','Sort$LocalUnitId')">BUR Nummer</a>
</th>