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


V4 Ajax Tutorials
Server Intellect Cloud Hosting

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.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
engraved dog tags said:

i am sure to all the commenter here! It's always nice when you can not only be informed, but also engaged! I'm sure you had fun writinI g this article.

Posted Nov 3, 2010 3:21 AM
exporters said:

sometimes i often got confused in doing the function calls.. often i got the errors on that..

Posted Nov 4, 2010 4:20 AM
Funeral Urns said:

being engaged and informed is a good process of learning..

Posted Nov 7, 2010 10:39 PM
Grossesse Semaine Par Semaine said:

ASP.NET 4 brings a lot of new possibilities over its predecessor, ASP.NET 3. One of the best additions is support for meta keywords and meta description. These are very important parts of a website if you want to rank high in search engines

Posted Nov 15, 2010 11:03 PM
Andalusien Netz said:

Thanks for the useful and detailed information =)

Posted Nov 18, 2010 9:57 AM
Reiseführer Comer See said:

Great and informative post!!! do like it!!! Thanks for the information shared!!!

Posted Nov 18, 2010 4:24 PM
holiday clothes said:

It’s quite hard to find a good website. And I am very satisfied to have come here. The publications are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update.

Posted Dec 1, 2010 10:24 AM
Indian Recipe said:

very creative effort it is more beneficial to users and makes their life more easy. Really, a genuine effort thanks.

Posted Dec 2, 2010 11:51 PM
Marathi Songs said:

these commands r very useful in many ways.

Posted Dec 5, 2010 2:03 AM
Malayalam Songs said:

AJAXtutorials became very useful in my work.Its methodology was very surprising.thanks again.

Posted Dec 7, 2010 5:02 AM
Mango said:

I don’t have anything else to include on to your article – you basically spelled everything out. great read!

Posted Dec 7, 2010 9:45 AM
Mango said:

I don’t have anything else to include on to your article – you basically spelled everything out. great read!

Posted Dec 7, 2010 9:46 AM
Trade Shows said:

This is a good post. This post give truly quality information.I’m definitely going to look into it.Really very useful tips are provided here.thank you so much.Keep up the good works.

Posted Dec 7, 2010 9:47 AM
Bengali Songs said:

These tutorial classes are made for everyone.

Posted Dec 8, 2010 6:10 AM
Mato Grosso said:

Totally endure the fact!!! Excellent writing!!!

Posted Dec 9, 2010 4:21 PM
Minas Gerais said:

Great post!!! Enormous thanks for the author!!!

Posted Dec 9, 2010 4:24 PM
Popular Videos said:

One of the best additions is support for meta keywords and meta description. These are very important parts of a website if you want to rank high in search engines

Posted Dec 30, 2010 10:41 PM
cakes for boys said:

I was very pleased to find this site. I wanted to thank you for this great read!! This is a very informative post, it helps me more.

Posted Jan 9, 2011 11:13 AM
memphis bankruptcy lawyer said:

To me, handle the Command Event property with Sorting is not easy

Posted Jan 10, 2011 2:39 AM
Wedding ceremony script said:

i am sure To All the comments here! It's Always Nice When You Can Not Only Be Informed, goal aussi engaged! I'm Sure You Had fun writin g this article. In addition a business that wishes to raise money on a stock market or to be owned by a wide range of people will often be required to adopt a specific legal form to do so.

Posted Jan 21, 2011 5:52 AM
RotoShave said:

It is a very useful and informative article indeed. It really help me to enhance my knowledge about different part of speech and understand these.

Posted Feb 16, 2011 3:04 AM
Crown or occipital area said:

Really good post.

Posted Feb 24, 2011 1:33 AM
cialis marseille said:

Thanks so much! I will keep checking your posts

Posted Mar 9, 2011 5:36 PM
geo tv live said:

Its quite hard to find a good website. And I am very satisfied to have come here. The publications are doing great and full of good insights. I would be glad to keep on coming back here to check for a new update.

Posted Mar 19, 2011 5:57 PM
Justin said:

Thanks for the great post! I've been thinking about this exact same thing over the last few days, so it's weird that you posted on it. You definitely did a much nicer job writing about it than I could have though.

Posted Apr 6, 2011 4:19 AM
Justin said:

Thanks for the great post! I've been thinking about this exact same thing over the last few days, so it's weird that you posted on it. You definitely did a much nicer job writing about it than I could have though.

Posted Apr 6, 2011 4:20 AM
propecia usa said:

The excellent tutorial. I admire with your knowledge.

Posted Jul 28, 2011 4:35 AM
www.outlet-louisvuitton.us said:

www.outlet-louisvuitton.us

Posted Aug 6, 2011 3:36 AM
SEO IN LOS ANGELES said:

Hey! I just want to give an enormous thumbs up for the good information you will have here on this post. I will be coming again to your weblog for more soon.

Posted Aug 16, 2011 2:01 PM
healthcare it said:

This site has lots of advantage.I found many interesting things from this site.It helps me many away. So many many thanks for sharing this information.I absolutely agree with you and thank you for pointing out several relevant and important examples. Several blog contributors have written excellently on this topic.Thanks for these sharing.

Posted Aug 17, 2011 4:28 AM
car insurance said:

Thanks for the great post! I've been thinking about this exact same thing over the last few days, so it's weird that you posted on it. You definitely did a much nicer job writing about it than I could have though.

Posted Aug 31, 2011 11:54 PM
car insurance said:

Hey! I just want to give an enormous thumbs up for the good information you will have here on this post. I will be coming again to your weblog for more soon.

Posted Aug 31, 2011 11:54 PM
dallas mortgage rates said:

I read the articles and all the things here inside this blog .,I got many information that.

I really need. Thanks for sharing.

Posted Sep 4, 2011 8:53 PM
hair salons ft lauderdale said:

You have shared a nice blog. I really want ot say you thanks for sharing this nice article with us. This one is enlightened blog post. Thanks a lot for sharing your valuable views through this blog. I bared so recurrent fascinating stuff in blog.

Posted Sep 5, 2011 3:44 AM
hair salons ft lauderdale said:

Each post I have read is very well written and to the point. I would also like to say, not only are the articles well written, but the design of your web-site is excellent. It was easy to navigate from post to post and find what I was looking for with ease. Keep up the great work you are doing, and I will return many times in the near future.

Posted Sep 5, 2011 3:46 AM
asd said:

efgdrhdr

Posted Sep 7, 2011 9:19 PM
Tim Burton said:

I had problems with Windows Dedicated Hosting last month but thanks to Server Intellect, all problems have been solved.

Posted Sep 8, 2011 11:18 PM
watchonlinetv said:

ou got a really useful blog I have been here reading for about an hour. I am a

newbie and your success is very much an inspiration for me.

Posted Sep 13, 2011 9:40 PM
Pool Vacuum Cleaner said:

I thought it was likely to be a few dull aged site, however I’ wren glad to visited. I will publish a link to this site upon my weblog. I am sure my visitors will discover which very helpful. ....

Posted Sep 29, 2011 6:05 AM
Follicular said:

I was newbie but after reading this post ,i have good knowledge of command Event Property.

Posted Sep 29, 2011 2:40 PM
event photography north west and north wales said:

This is a good post. I'm definitely going to look into it.Really very useful tips are provided here.thank you so much.Keep up the good works.

 

Posted Oct 5, 2011 12:48 AM
Family Lawyer Brampton said:

This is a great blog posting and very useful. I really appreciate the research you put into it...

Posted Oct 8, 2011 3:20 AM
helen olsson said:

Good work! I always like to leave comments whenever I see something unusual or impressive. I think we must appreciate those who do something especial. Keep it up, thanks

Helen Olsson.

Posted Oct 30, 2011 4:09 AM
computer said:

I just came across your blog and reading your beautiful words. I thought I would leave my first comment but I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Helen Olsson

Posted Dec 3, 2011 12:52 PM
childhood anxiety treatment said:

This is such a beautiful tutorial. Love this so much.

Posted Jan 5, 2012 10:45 AM
childhood anxiety treatment said:

Love this blog and its tutorials.

Posted Jan 5, 2012 10:45 AM

Leave a Comment