Monday, March 14, 2011

ListView Control in ASP.NET 3.5 – 3

This post is a continuing sample demonstration of these two posts:
• ListView Control in ASP.NET 3.5 – 1 (opens a new window)
• ListView Control in ASP.NET 3.5 – 2 (opens a new window)
It would be a good practice to read them first before jumping into this post.
Using EditItemTemplate with ItemCommand
I will try to show how to add an editing capability to the ListView in this post by utilizing ItemCommand event. This event is basically raised when a button inside the ListView is clicked.

Following code should be added inside the ItemTemplate and AlternatingItemTemplate templates:

After adding the above code, EditItemTemplate template should also be placed somewhere between the ListView tags like the other templates:

Even though Edit, Update, Cancel and Delete buttons have built-in functionalities about what they are supposed to be doing, I will only use this advantage for Edit and Cancel buttons. Edit button puts the row in an edit mode by rendering the EditItemTemplate that I defined above. Cancel button invokes its built-in functionality which cancels the edit operation by raising the ItemCanceling event. For other buttons, I prefer to write their own custom routines:

As a result of these modifications, the result on the browser should look like:

When the Edit button is clicked:

Remember that I put RequiredFieldValidator controls for Country Name and Capital City textboxes (look at the 3rd screenshot in this post). Therefore, any operation in the edit mode (Update, Cancel or Delete) will not take action if one of these or both fields are blank:

For more understanding of this post, you can download the sample (by VWD 2008) via here.

No comments:

Popular Posts