Learn how to handle the Command Event property with Sorting. C#


V4 Ajax Tutorials

Learn how to handle the Command Event property with Sorting. C#

This tutorial will cover how to handle the Command Event property using ASP.NET C#

In this tutorial we will cover how the handle Command Event by implementing two button controls to sort; ascending and descending. In this case, the two button controls will list items displayed by the BulletedList control when clicked.

Here is a snapshot of how the application will look like:

Need help with Windows Dedicated Hosting? Try Server Intellect I'm a happy customer!

To start, open Visual Studio 2008. File > New > Web site > ASP.NET Web site and name it sort or whatever you want. Make sure that you include using System.Collections.Generic;

From the Design View, drag over two button controls and assign them the values listed below:

<asp:Button ID="btnSortAsc"
Text="Sort ASC"
CommandName="Sort"
CommandArgument="ASC"
OnCommand="Sort_Command"
runat="server" />
asp:Button ID="btnSortDESC"
Text="Sort DESC"
CommandName="Sort"
CommandArgument="DESC"
OnCommand="Sort_Command"
runat="server" />

Notice how both Button controls include CommandName and CommandArgument properties. Additionally, we have both Button controls wired to the same Sort_Command() event handler.

Next, from the Design View locate the standard tab of the Toolbox, and drag over a bulleted list. Assign it the same value as below:

<asp:BulletedList ID="bltPeople" runat="server" />

Switch to your code behind and let’s add some logic for these event handlers.

First, lets populate the page when it loads with these values below. So within the Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
people.Add("Mike");
people.Add("Edward");
people.Add("Thomas");
}

This lets our page know that we have a list of data and we need to add things to it.

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

Next, we need to add an event handler that checks the CommandName and CommandArguement properties when determining how the elements in the BulletedList should be sorted.

protected void Sort_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Sort")
{
switch (e.CommandArgument.ToString())
{
case "ASC": people.Sort(SortASC);
break;
case "DESC": people.Sort(SortDESC);
break;
}
}
}

Above we created a Command that will determine which function to call during data organization.

The following part of our script is required in order to work correctly. The funciton Page_PreRender() lets the page right before it starts processing that our bltPeople.DataSource = people;. Keep in mind that when we created: private List people = new List(); that we told our page that our list of strings is going to be used in our Bulleted List.

void Page_PreRender()
{
bltPeople.DataSource = people; bltPeople.DataBind();
}

Finally, we have our sort functions. The Sort_Command was designed to handle both button requests for ASC and DESC order. Since we know what we want to do with each function we simply add a basic String.Compare statement that compares two variables and returns them in a given sequence order.

int SortASC(string x, string y)
{
return String.Compare(x, y);
}
int SortDESC(String x, String y)
{
return String.Compare(x, y) * -1;
}

If you're ever in the market for some great Windows web hosting, try Server Intellect.We have been very pleased with their services and most importantly, technical support.



Comments

There are no comments yet...Kick things off by filling out the form below.


Leave a Comment