ObjectDataSourc用法之三(排序)

來源:互聯網
上載者:User

 

ObjectDataSourc用法之三(排序)

SortParameterName參數主要用於對數據原始檔控制進尾排序

1.       准備條件

參數:ObjectDataSource用法之一(SelectMethod來進行簡單的邦定)

添加一個處理對象排序的類Reverser

public class Reverser<T> : IComparer<T>

{

    private Type type = null;

    private ReverserInfo info;

    public Reverser(string className, string name, ReverserInfo.Direction direction)

    {

        try

        {

            this.type = Type.GetType(className, true);

            this.info.name = name;

            this.info.direction = direction;

        }

        catch (Exception e)

        {

            throw new Exception(e.Message);

        }

    }

    int IComparer<T>.Compare(T t1, T t2)

    {

        object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);

        object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);

        if (this.info.direction != ReverserInfo.Direction.ASC)

            Swap(ref x, ref y);

        return (new CaseInsensitiveComparer()).Compare(x, y);

    }

    private void Swap(ref object x, ref object y)

    {

        object temp = null;

        temp = x;

        x = y;

        y = temp;

    }

}

 

public struct ReverserInfo

{

    public enum Direction

    {

        ASC = 0,

        DESC,

    };

    public enum Target

    {

        CUSTOMER = 0,

        FORM,

        FIELD,

        SERVER,

    };

    public string name;

    public Direction direction;

    public Target target;

}

2.       在業務處理類中添加如下方法

public List<EntityMember> OrderItems(string order)

{

    string orderName = order.Split(' ')[0];

    ReverserInfo.Direction dir = ReverserInfo.Direction.ASC;

    if (order.Split(' ').Length>1 && order.Split(' ')[1] == "DESC") dir = ReverserInfo.Direction.DESC;

    List<EntityMember> result = new List<EntityMember>();

    XmlDocument doc = new XmlDocument();

    doc.Load(_path);

    XmlNodeList nodes = doc.SelectNodes("/Members/Member");

    foreach (XmlNode node in nodes)

    {

        result.Add(new EntityMember(node.SelectSingleNode("./UID").InnerText, node.SelectSingleNode("./PWD").InnerText, node.SelectSingleNode("./Email").InnerText));

    }

    Reverser<EntityMember> reverser = new Reverser<EntityMember>("EntityMember", orderName, dir);

    result.Sort(reverser);

    return result;

 

說明:當按降序排列的時候,參數order的內容為:屬性名稱+空格+DESC

       當按升序排列的時候,參數order的內容為:屬性名稱

3.       Aspx頁面的內容為

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"

    SelectMethod="OrderItems" SortParameterName="order" TypeName="Member"></asp:ObjectDataSource>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

    DataSourceID="ObjectDataSource1" AllowSorting="true">

    <Columns>

        <asp:BoundField DataField="UID" HeaderText="UID" SortExpression="UID" />

        <asp:BoundField DataField="PWD" HeaderText="PWD" SortExpression="PWD" />

        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />

    </Columns>

</asp:GridView>

說明:SortParameterName為指定SelectMethod參數指定的方法中用於排序的參數名稱

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.