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:

Download
For more understanding of this post, you can download the sample (by VWD 2008) via here.
Update
This post is continued by:
Do you know how you can have the RequiredFieldValidator only enabled for the Update button?
Cheers!
Kim
February 8, 2008 at 1:38 PM
Yes, it can be accomplished by the following steps:
1. Set the Enabled property of a RequiredFieldValidator to False as default (in aspx page).
2. Enable it depending on your conditions within the ItemCommand event, such as:
If e.CommandName = “Update” Then RequiredFieldValidator.Enabled = True
Mustafa Basgun
February 8, 2008 at 4:32 PM
Hi, thanks. I will try this out.
In my ListView, some of the columns are empty, and when they are empty I get no border around them. If the column contains a data value, it gets a border. Do you have any idea how I can get a border around empty columns?
Cheers,
Kim
Kim
February 8, 2008 at 9:40 PM
Kim,
You can put a non-breaking space when a specific column value is empty by using IIf function in VB.NET. Assuming that your column name is CountryName, the scenario may be like this:
Before
<%#Eval(“CountryName”)%>
After
<%#IIf(Eval(“CountryName”).ToString.Trim = “”, “ ”, Eval(“CountryName”))%>
I have not tested it, therefore try it at your own risk.
FYI, The equivalent of IIf function in C# is called Ternary Operator.
Update
If a specific column value that you are talking about is Null, then use IsDBNull function in your IIf statement.
Mustafa Basgun
February 8, 2008 at 11:31 PM
How would you handle a ListBox control instead of a TextBox?
I get the following error:
Unable to cast object of type ListBox to type TextBox
TIA
Cheryl
November 11, 2008 at 12:01 PM
Following sample should work for you:
Dim ListBox1 As ListBox
ListBox1 = CType(e.Item.FinControl(“YourListBoxID”), ListBox)
Mustafa Basgun
November 15, 2008 at 8:04 AM