我正在研究 aListView
并且感觉在实现排序时我在做一些根本错误的事情。我无法依赖 and 的值List1.SortExpression
,List1.SortDirection
而是求助于隐藏字段,因为List1.SortExpression
它始终为空白且List1.SortDirection
始终为SortDirection.Ascending
。
在我的 .aspx 页面上:(编辑掉不相关的代码)
<asp:HiddenField runat="server" ID="hdnSortExpression" />
<asp:HiddenField runat="server" ID="hdnSortDirection" />
<asp:ListView runat="server" ID="List1"
OnItemCommand="List1_ItemCommand"
OnSorting="List1_Sorting">
<LayoutTemplate>
<table border="0" cellpadding="1">
<thead>
<tr>
<th><asp:LinkButton runat="server" ID="BtnCompanyCode" CommandName="Sort" CommandArgument="CompanyCode" Text="Company Code" /></th>
... more columns ...
</tr>
</thead>
<tbody>
<tr runat="server" id="itemPlaceholder"></tr>
</tbody>
</table>
</LayoutTemplate>
在我的代码后面:
protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// empty for now
}
protected void List1_Sorting(object sender, ListViewSortEventArgs e)
{
SortDirection sortDirection;
String sortExpression = e.SortExpression; // how we need to do it in Sorting
if (hdnSortExpression.Value.ToLower() == sortExpression.ToLower())
sortDirection = hdnSortDirection.Value == SortDirection.Ascending.ToString() ? SortDirection.Descending : SortDirection.Ascending;
else
sortDirection = SortDirection.Ascending;
DoSortList(sortExpression, sortDirection); // this sets column headings' sort indicator arrows
List1_BindData(sortExpression, sortDirection); // sets DataSource and calls DataBind()
// the hacky part: setting hidden fields to store sortExpression and sortDirection
hdnEligibilitySortExpression.Value = sortExpression;
hdnEligibilitySortDirection.Value = sortDirection.ToString();
}
private void List1_BindData(String sortExpression, SortDirection sortDirection)
{
List<SomeEntity> list = null;
list = SomeEntity.GetBySsn(_ssn).ToList(); // methods generated by an ORM pointing to an Oracle database
switch (sortExpression.ToLower())
{
case "CompanyCode":
if (sortDirection == SortDirection.Ascending)
List1.DataSource = list.OrderBy(o => o.CompanyCode);
else
List1.DataSource = list.OrderByDescending(o => o.CompanyCode);
break;
... other cases ...
}
List1.DataBind();
}
它可以正常工作——每一列都被正确排序,单击同一列会反转排序方向,但我必须得出结论,因为我不能依赖SortDirection
andSortExpression
属性,所以我的连接不正确。我究竟做错了什么?