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.
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?
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
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.
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.
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.
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.
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:
I also have an empty event handler at the top (because the user control throws an error without it):
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?
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?
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
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.
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
Hi Joey,
I ended up doing the following now:
and in the code-behind:
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
Okay, editing deletes unfortunately the first code, so here it is again:
Ah hah! Success!
All I needed was one line of your code
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.
Fantastic news, glad it worked out in the end. :)
Cheers,
Sascha
is it just me, or are you not familiar with Linq? You should be able to do this perfectly
or
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.
is working on a reply...