gpt4 book ai didi

ASP.NET GridView排序升序标题样式不起作用

转载 作者:行者123 更新时间:2023-12-04 05:08:45 26 4
gpt4 key购买 nike

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

最佳答案

如果您不使用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;
}

关于ASP.NET GridView排序升序标题样式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7681944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com