Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Joey Kincer 51 posts 83 karma points
    Aug 13, 2010 @ 01:18
    Joey Kincer
    0

    Sorting a list of members inside a .NET user control

    I'm having a heck of a time trying to figure this out.

    I'm using the old businesslogic API to databind a ListView control with the Member.GetAll function. While this works fine and I get my list of members, I cannot for the life of me figure out how to alphabetically sort by LoginName or Email on page load. (I don't want to use a button click event to sort, this needs to happen on page load.) I'm trying to use the ListView.Sort() method but it seems I have to use some kind of Event Handler to make it work.

    This function is called during page load:

    public static void GetAllMembersList(ListView lvAllMembers)
    {
        var allMembers = Member.GetAll;
        lvAllMembers.Sort("LoginName", SortDirection.Ascending);
        lvAllMembers.DataSource = allMembers;
        lvAllMembers.DataBind();            
    }
    

    I also have an empty event handler at the top (because the user control throws an error without it):

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.rblMemberType.SelectedIndexChanged += new EventHandler(rblMemberType_SelectedIndexChanged);
        this.lvMembers.Sorting += new EventHandler(lvMembers_Sorting);
        this.lvMembersApproved.Sorting += new EventHandler(lvMembers_Sorting);
        this.lvMembersPending.Sorting += new EventHandler(lvMembers_Sorting);
    }
    
    protected void lvMembers_Sorting(object sender, ListViewSortEventArgs e)
    {
    
    }
    

    Although the binding is going through and listing all the members, the list stays unsorted, no matter where I place it in the function.

    I've tried to find tutorials on this, but all they talk about is using button click events. The MSDN Library states that "This method is also used to programmatically set a default sort order for the ListView control when it is first rendered." but I sure can't figure out how to do it.

    Can anyone lend me some pointers?

  • Joey Kincer 51 posts 83 karma points
    Aug 13, 2010 @ 01:25
    Joey Kincer
    0

    Since I can't edit my post (it keeps throwing an XSLT error) here's something I wanted to add:

    I'm wondering if I'm going about this whole thing the wrong way, and there's some way to sort the member list using the businesslogic API prior to binding it as a DataSource to the ListView?

  • Sascha Wolter 615 posts 1101 karma points
    Aug 13, 2010 @ 02:41
    Sascha Wolter
    0

    Hi Joey,

    apparently you are responsible for sorting the data and re-binding it to the control if you bind the datasource manually in your code. Maybe this will help if you haven't found it already: http://www.4guysfromrolla.com/articles/011608-1.aspx

    Let me know how it goes.

    Sascha

  • Joey Kincer 51 posts 83 karma points
    Aug 13, 2010 @ 06:24
    Joey Kincer
    0

    Thanks Sascha. That is one of the many articles I have found regarding sorting a ListView.

    I take it the section Creating an Event Handler for the ListView's Sorting Event is what I'm supposed to be following, but they don't have any example C# code that I can reference, even in the demo I downloaded from that page. And they don't talk specifically about sorting the listview after it's been binded (they keep going back to talking about buttons).

    I already have a PreRender event handler on the listview that binds the Members. Not sure how I can dove tail this Sorting event handler to it.

     

  • Sascha Wolter 615 posts 1101 karma points
    Aug 13, 2010 @ 10:43
    Sascha Wolter
    0

    Grr, thought it might solve it. I've set up a small sample project myself with the list view, funnily enough when it comes to calling the sorting event handler everythings seems to be in order ('LoginName' set as sort expression, order descending set), yet nothing happens. That's why I somehow think you'd need to add some kind of comparer so it knows how to sort (hope you know what I mean).

    Anyway, in terms of finding another solution: would it also be acceptable for you to sort the items before binding them to the list view? Do you need the sorting functionality afterwards for the user? If the user can't sort the list and you just use it to display then it seems a lot of work now (unfortunately) to get the sort on render of list view to work. I'll have a bit of a play myself.

    Sascha

  • Sascha Wolter 615 posts 1101 karma points
    Aug 13, 2010 @ 11:13
    Sascha Wolter
    0

    Hi Joey,

    I ended up doing the following now:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MemberList.ascx.cs" Inherits="MembersListView.MemberList" %>
    
    
            

    ===============

    ===============

    and in the code-behind:

           protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindMembers(SortDirection.Descending);
                }
            }

            private void BindMembers(SortDirection direction)
            {
                var allMembers = Member.GetAll;
                switch (direction)
                {
                    case SortDirection.Ascending:
                        Array.Sort(allMembers, delegate(Member m1, Member m2) { return m1.LoginName.CompareTo(m2.LoginName); });
                        break;
                    case SortDirection.Descending:
                        Array.Sort(allMembers, delegate(Member m1, Member m2) { return m2.LoginName.CompareTo(m1.LoginName); });
                        break;
                }

                lvAllMembers.DataSource = allMembers;
                lvAllMembers.DataBind();
            }

            protected void lvAllMembers_Sorting(object sender, ListViewSortEventArgs e)
            {
                BindMembers(e.SortDirection);
            }

            protected void btnSort_Click(object sender, EventArgs e)
            {
                SortDirection direction = SortDirection.Ascending;
                if (ddlDirection.SelectedValue == "2")
                    direction = SortDirection.Descending;

                lvAllMembers.Sort("LoginName", direction);
            }

    This is just a quick mock-up and no beautiful code, most importantly you would need to cache the members array so you don't have to call Member.GetAll all the time.

    Give it a go and let me know please.

    Sascha

  • Sascha Wolter 615 posts 1101 karma points
    Aug 13, 2010 @ 11:16
    Sascha Wolter
    0

    Okay, editing deletes unfortunately the first code, so here it is again:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MemberList.ascx.cs" Inherits="MembersListView.MemberList" %>
    <asp:ListView ID="lvAllMembers" runat="server" onsorting="lvAllMembers_Sorting">
        <ItemTemplate>
            <p>
                ===============<br />
                <asp:Label runat="server" ID="lblLoginName" Text='<%# Eval("LoginName") %>' /><br />
                ===============<br />
            </p>
        </ItemTemplate>
    </asp:ListView>
    <asp:DropDownList runat="server" ID="ddlDirection">
        <asp:ListItem Value="1" Text="asending"></asp:ListItem>
        <asp:ListItem Value="2" Text="descending"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button runat="server" ID="btnSort" Text="sort" onclick="btnSort_Click" />
  • Joey Kincer 51 posts 83 karma points
    Aug 13, 2010 @ 18:09
    Joey Kincer
    0

    Ah hah! Success!

    All I needed was one line of your code

    public static void GetAllMembersList(ListView lvAllMembers)
    {
        var allMembers = Member.GetAll;
        Array.Sort(allMembers, delegate(Member m1, Member m2) { return m1.LoginName.CompareTo(m2.LoginName); });
        lvAllMembers.DataSource = allMembers;
        lvAllMembers.DataBind();
    }
    

    To answer your question, yes it is most definitely acceptable to sort the order before binding. There are no buttons on the page that control the sorting. I just wanted it to sort on page load and nothing else.

    I'm not too familiar with the Array datatype, so I didn't know about its sorting capabilities. Not sure if this method is resource-intensive, but even if it is, there's less than 300 members so I'm not really worried about it.

    Thank you so much! I can put 6 hours of frustration behind me.

  • Sascha Wolter 615 posts 1101 karma points
    Aug 13, 2010 @ 18:36
    Sascha Wolter
    0

    Fantastic news, glad it worked out in the end. :)

    Cheers,
    Sascha

  • Pauli Østerø 6 posts 28 karma points
    Aug 14, 2010 @ 00:13
    Pauli Østerø
    2

    is it just me, or are you not familiar with Linq? You should be able to do this perfectly

     

    var allMembers = Member.GetAll.Sort(m => m.LoginName);
    or
    var allMembers = Member.GetAll.Cast<Member>().Sort(m => m.LoginName);
    lvAllMembers.DataSource = allMembers;
    lvAllMembers
    .DataBind();

  • Joey Kincer 51 posts 83 karma points
    Aug 14, 2010 @ 05:04
    Joey Kincer
    0

    I haven't trained myself in LINQ, so I'm not yet aware of its capabilities. I'll have to look into that. Thanks for mentioning it.

Please Sign in or register to post replies

Write your reply to:

Draft