我的GridView控件出现问题。在对DataBind进行后期排序之后,我无法将我的css类保留在已排序的标题上。
我的GridView:
<asp:GridView CssClass="grid-view shadow" ID="GridView1" runat="server" Caption="Application Owners" AutoGenerateColumns="false" PageSize="10" ShowHeaderWhenEmpty="true" EmptyDataText="No information found." AllowSorting="true" AllowPaging="true" OnSorting="GridView1_Sorting" >
<PagerSettings FirstPageText="First" PreviousPageText="Previous" NextPageText="Next" LastPageText="Last" Visible="False" />
<HeaderStyle CssClass="header" />
<FooterStyle ForeColor="MediumBlue" BackColor="LightCyan" />
<RowStyle CssClass="odd-row" />
<AlternatingRowStyle CssClass="even-row" />
<SortedAscendingHeaderStyle CssClass="sort-asc" />
<SortedDescendingHeaderStyle CssClass="sort-desc" />
<Columns>
<asp:BoundField DataField="ClientName" HeaderText="Client" SortExpression="ClientName" />
<asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
<asp:BoundField DataField="HostName" HeaderText="HostName" SortExpression="HostName" />
<asp:BoundField DataField="InstanceName" HeaderText="Instance" SortExpression="InstanceName" />
<asp:BoundField DataField="SystemType" HeaderText="SystemType" SortExpression="SystemType" ItemStyle-Wrap="True" />
<asp:BoundField DataField="DatabaseName" HeaderText="Database" SortExpression="DatabaseName" />
<asp:BoundField DataField="BackupStrategyName" HeaderText="Strategy" SortExpression="BackupStrategyName" />
<asp:BoundField DataField="BackupType" HeaderText="Type" SortExpression="BackupType" />
<asp:BoundField DataField="BackupLocation" HeaderText="Location" SortExpression="BackupLocation" />
<asp:BoundField DataField="EngineName" HeaderText="Engine" SortExpression="EngineName" />
<asp:BoundField DataField="Comments" HeaderText="SequenceType" SortExpression="Comments" />
<asp:BoundField DataField="LastModified" HeaderText="LastModified" SortExpression="LastModified" />
</Columns>
</asp:GridView>
由于我使用DataTable作为源,我将sort表达式应用于它并使用其属性GridView1.DefaultView将其重新绑定到我的GridView。
我的排序方法:
public void GridViewSort(object sender, GridViewSortEventArgs e)
{
SortDirection _sortDirection;
SortDirection _lastSortDir = SortDirection.Ascending;
string _sortDir;
string _cssClass;
int _columnIndex = 0;
// Get the last sort direction from the DataTable`s DefaultView property.
if (tempTable.DefaultView.Sort.ToString().Contains("ASC"))
{
_lastSortDir = SortDirection.Ascending;
}
else if (tempTable.DefaultView.Sort.ToString().Contains("DESC"))
{
_lastSortDir = SortDirection.Descending;
}
// Check if it`s the first time sorting the GridView.
if (String.IsNullOrEmpty(tempTable.DefaultView.Sort.ToString()))
{
_sortDirection = SortDirection.Ascending;
}
else
{
// If the sort is on the same column as the previous sort then change the sorting order.
if (tempTable.DefaultView.Sort.ToString().Contains(e.SortExpression.ToString()))
{
_sortDirection = (_lastSortDir == SortDirection.Ascending) ? SortDirection.Descending : SortDirection.Ascending;
}
// If it`s a different column then sort it in Ascending order.
else
{
_sortDirection = SortDirection.Ascending;
}
}
// Set the DefaultView.Sort propery of the DataTable and rebind it to the GridView.
// Add the sort oreder icon to the filtered header.
_sortDir = (_sortDirection == SortDirection.Ascending) ? "ASC" : "DESC";
_cssClass = (_sortDirection == SortDirection.Ascending) ? "sort-asc" : "sort-desc";
tempTable.DefaultView.Sort = e.SortExpression.ToString() + " " + _sortDir.ToString();
GridView1.DataSource = tempTable;
GridView1.DataBind();
// Find GridView header and add sorting image class
foreach (DataControlFieldHeaderCell headerCell in GridView1.HeaderRow.Cells)
{
if (headerCell.ContainingField.SortExpression == e.SortExpression)
{
_columnIndex = GridView1.HeaderRow.Cells.GetCellIndex(headerCell);
}
}
GridView1.HeaderRow.Cells[_columnIndex].CssClass = _cssClass;
}
直到这一点,它就像一个魅力。我的问题是,当我转到下一页或做任何需要新的DataBind()到GridView的操作时,标头的css类被删除。我对C#比较新,所以如果这是一个可能的解决方案,我不知道如何处理视图状态。谢谢!