我不知道为什么每个人都忘记使用隐藏字段!它们比ViewState(我从2005年起就关闭它)要便宜得多。如果您不想使用Session或ViewState,那么这是我的解决方案:
将这两个隐藏的字段放在aspx页面上,然后将所需的默认排序作为数据(例如,我使用的是LastName):
<asp:HiddenField ID="hfSortExpression" runat="server" Value="LastName" />
<asp:HiddenField ID="hfSortDirection" runat="server" Value="Ascending" />
然后将此帮助程序代码放在“基本”页面中(您有基本页面吗?如果没有,请在.cs代码后面放置)。
protected SortDirection GetSortDirection(GridViewSortEventArgs e, HiddenField hfSortExpression, HiddenField hfSortDirection)
{
SortDirection sortDirection = SortDirection.Ascending;
string previousSortExpression = hfSortExpression.Value;
SortDirection previousSortDirection = !string.IsNullOrEmpty(hfSortDirection.Value) ? ((SortDirection)Enum.Parse(typeof(SortDirection), hfSortDirection.Value)) : SortDirection.Ascending;
if (e.SortExpression == previousSortExpression)
{
if (previousSortDirection == SortDirection.Ascending)
{
sortDirection = SortDirection.Descending;
}
}
hfSortExpression.Value = e.SortExpression;
hfSortDirection.Value = sortDirection.ToString();
return sortDirection;
}
接下来,您需要在网格排序事件处理程序中处理排序。在调用获取数据的主方法之前,请从排序事件处理程序中调用上述方法
protected void gridContacts_Sorting(object sender, GridViewSortEventArgs e)
{
SortDirection sortDirection = GetSortDirection(e, hfSortExpression, hfSortDirection);
GetCases(_accountId, e.SortExpression, sortDirection);
}
由于那里有很多示例都使用DataTables或DataViews或其他非LINQ友好集合,因此我想我将包括一个示例,该示例调用中间层方法,该方法返回通用列表,并使用LINQ进行排序以求四舍五入该示例并使它更“真实”:
private void GetCases(AccountID accountId, string sortExpression, SortDirection sortDirection)
{
List<PendingCase> pendingCases = MyMiddleTier.GetCasesPending(accountId.Value);
lblCountPendingCases.Text = pendingCases.Count.ToString();
pendingCases = Sort(sortExpression, sortDirection, pendingCases);
grid.DataSource = pendingCases;
grid.DataBind();
}
最后,这是在常规自定义对象列表上使用LINQ进行的繁琐排序。我敢肯定,有一些幻想者会解决这个问题,但这说明了这个概念:
私有静态List Sort(字符串sortExpression,SortDirection sortDirection,列表未决案例){
switch (sortExpression)
{
case "FirstName":
pendingCases = sortDirection == SortDirection.Ascending ? pendingCases.OrderBy(c => c.FirstName).ToList() : pendingCases.OrderByDescending(c => c.FirstName).ToList();
break;
case "LastName":
pendingCases = sortDirection == SortDirection.Ascending ? pendingCases.OrderBy(c => c.LastName).ToList() : pendingCases.OrderByDescending(c => c.LastName).ToList();
break;
case "Title":
pendingCases = sortDirection == SortDirection.Ascending ? pendingCases.OrderBy(c => c.Title).ToList() : pendingCases.OrderByDescending(c => c.Title).ToList();
break;
case "AccountName":
pendingCases = sortDirection == SortDirection.Ascending ? pendingCases.OrderBy(c => c.AccountName).ToList() : pendingCases.OrderByDescending(c => c.AccountName).ToList();
break;
case "CreatedByEmail":
pendingCases = sortDirection == SortDirection.Ascending ? pendingCases.OrderBy(c => c.CreatedByEmail).ToList() : pendingCases.OrderByDescending(c => c.CreatedByEmail).ToList();
break;
default:
break;
}
return pendingCases;
}
最后但并非最不重要的一点(我已经说过吗?),您可能想要在Page_Load处理程序中放置类似的内容,以便默认情况下在页面加载时网格会绑定...请注意,_accountId是一个querystring参数,已转换为自定义在这种情况下,我自己的AccountID类型...
if (!Page.IsPostBack)
{
//sort by LastName ascending by default
GetCases(_accountId,hfSortExpression.Value,SortDirection.Ascending);
}