ASP.NET GridView SortedAscendingHeaderStyle 不起作用 [英] ASP.NET GridView SortedAscendingHeaderStyle does not work
问题描述
我的 SortedAscendingHeaderStyle
和 SortedDescendingHeaderStyle
根本不起作用
My SortedAscendingHeaderStyle
and SortedDescendingHeaderStyle
is not working at all
<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 检查标题时,它只显示:(这是升序排序时)
Rows are sorted correctly when headers are clicked, but when I inspect the header using FireBug, it only shows: (this is when sorted ascending)
<th scope="col">
<a href="javascript:__doPostBack('ctl00$body$ctl00$grdProducts','Sort$Namekey')">Namekey</a>
</th>
根本没有设置 ForeColor 和 CssClass.
ForeColor and CssClass are not set at all.
有人知道我做错了什么吗?
Anyone has any idea what I am doing wrong?
我背后的 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
SortedDescendingHeaderStyle 是否可以在没有代码的情况下工作> 作为您的 GridView 数据源.但是一点点编码就可以让你达到目标.
I'm not sure if SortedDescendingHeaderStyle works without code if you're not using an asp:SQLDataSource
as your GridView data source. But a little coding can get you there.
您需要手动将 CSS 样式应用于标题单元格.您可以在 Sorting 事件中进行.
You need to apply the CSS style manually to the header cell. You can do it in the Sorting event.
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;
}
这篇关于ASP.NET GridView SortedAscendingHeaderStyle 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!