Personal Blog of Mustafa Basgun

ListView Control in ASP.NET 3.5 – 3

with 6 comments

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.


This post is continued by:

ListView Control in ASP.NET 3.5 – 4


Written by Mustafa Basgun

December 29, 2007 at 5:00 PM

6 Responses

Subscribe to comments with RSS.

  1. Do you know how you can have the RequiredFieldValidator only enabled for the Update button?



    February 8, 2008 at 1:38 PM

  2. 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

  3. 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?




    February 8, 2008 at 9:40 PM

  4. 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:




    <%#IIf(Eval(“CountryName”).ToString.Trim = “”, “&nbsp;”, 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.


    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

  5. 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



    November 11, 2008 at 12:01 PM

  6. 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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: