4

私はに取り組んでおり、ListViewソートの実装に関しては根本的に間違ったことをしていると感じています。List1.SortExpressionとの値に依存できるのではなく、常に空白であり、常にでList1.SortDirectionあるため、非表示のフィールドに頼っています。List1.SortExpressionList1.SortDirectionSortDirection.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ます。私は何が間違っているのですか?

4

2 に答える 2

2

このMSDNの例を見てください。DataSourceがSqlDataSourceであることがわかります。これは、.Netがソート方法を理解するデータソースタイプの1つです。その他、それほど多くはありません。

DataSourceタイプを指定する別のMSDN記事は次のとおりです。

並べ替えの組み込みサポートを提供するデータソースコントロールは、LinqDataSource、ObjectDataSource、SqlDataSource、およびAccessDataSourceコントロールです。

于 2012-10-03T16:55:30.543 に答える
0

e.SortDirectionを使用するのではなく、List1.SortDirectionやList1.SortExpressionのようにListViewからプルできますか?

于 2012-10-03T16:42:59.643 に答える