Yet Another Blog on ASP.NET and Some Other Stuff

ListView Control in ASP.NET 3.5 - 3

Posted in ASP.NET, ListView, SQL, VB.NET, Web Development by Mustafa Basgun on December 29th, 2007

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:

ListView Control in ASP.NET 3.5 - 4

4 Responses to 'ListView Control in ASP.NET 3.5 - 3'

Subscribe to comments with RSS or TrackBack to 'ListView Control in ASP.NET 3.5 - 3'.

  1. Kim said, on February 8th, 2008 at 1:38 pm

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

    Cheers!

  2. Mustafa Basgun said, on February 8th, 2008 at 4:32 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

  3. Kim said, on February 8th, 2008 at 9:40 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

  4. Mustafa Basgun said, on February 8th, 2008 at 11:31 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 = “”, “&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.

    Update

    If a specific column value that you are talking about is Null, then use IsDBNull function in your IIf statement.

Leave a Reply