私はに取り組んでおり、ListView
ソートの実装に関しては根本的に間違ったことをしていると感じています。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
それは機能します-各列は正しくソートされ、同じ列をクリックするとソート方向が逆になりますが、プロパティに依存できないため、正しく接続されていないと結論付ける必要がありSortExpression
ます。私は何が間違っているのですか?