问题描述
我的 SortedAscendingHeaderStyle
和 SortedDescendingHeaderStyle
都不能正常工作
< ASP:GridView控件ID =grdProducts=服务器的CssClass =网格AllowPaging =真AllowSorting =真每页=100EmptyDataText =没有资料证明
onrowdatabound =grdProducts_RowDataBoundonrowediting =grdProducts_RowEditingonsorting =grdProducts_SortingAutoGenerateEditButton属性=真>
< AlternatingRowStyle的CssClass =甚至/>
< SortedAscendingHeaderStyle前景色=白的CssClass =分类/>
< SortedDescendingHeaderStyle的CssClass =分类说明/>
< / ASP:GridView的>
行排序正确被点击头时,但是当我检查使用Firebug的头,那只能说明:(这是当将按升序排序)
百分位范围=山口>
&所述; A HREF =JavaScript的:__ doPostBack('ctl00 $体$ ctl00 $ grdProducts','排序$ Namekey')> Namekey&下; / A>
< /第i
前景色和CssClass属性没有设定的。
任何人有任何想法,我做错了?
编辑:背后
我的C#code 保护无效grdProducts_Sorting(对象发件人,GridViewSortEventArgs E)
{
如果((串)的ViewState [SortColumn] == e.SortEx pression)
的ViewState [SortDirection] =((串)的ViewState [SortDirection] ==)? DESC:;
其他
{
的ViewState [SortColumn] = e.SortEx pression;
的ViewState [SortDirection] =;
}
} 保护覆盖无效在preRender(EventArgs的发送)
{
BindGrid();
base.On preRender(E);
} 私人无效BindGrid()
{
查询字符串=SELECT ... ORDER BY+的ViewState [SortColumn] +的ViewState [SortDirection]; DataTable的DT = SqlFunctions.Select(查询);
grdProducts.DataSource = DT;
grdProducts.DataBind();
}
我不知道,如果 SortedDescendingHeaderStyle 如果您使用的不是没有code工作的 ASP:SqlDataSource的
为你的GridView的数据源。但有点可以编码让你有。
您需要手动应用CSS样式的标题单元格。你可以做到这一点的Sorting事件。
保护无效grdProducts_Sorting(对象发件人,GridViewSortEventArgs E)
{
如果((串)的ViewState [SortColumn] == e.SortEx pression)
{
的ViewState [SortDirection] =((串)的ViewState [SortDirection] ==)? DESC:;
grdProducts.HeaderRow.Cells [GetColumnIndex(e.SortEx pression)的CssClass =AscendingHeaderStyle。
}
其他
{
的ViewState [SortColumn] = e.SortEx pression;
的ViewState [SortDirection] =;
。grdProducts.HeaderRow.Cells [GetColumnIndex(e.SortEx pression)的CssClass =DescendingHeaderStyle;
}
BindGrid();
}
私人诠释GetColumnIndex(字符串SORTEX pression)
{
INT I = 0;
的foreach(在gvwCustomers.Columns的DataControlField C)
{
如果(c.SortEx pression == SORTEX pression)
打破;
我++;
}
返回我;
}
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>
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 and CssClass are not set at all.
Anyone has any idea what I am doing wrong?
EDIT: My C# code behind
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();
}
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.
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不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!