Data Manipulation using ListView Server Control with ASP.NET 3.5
page 5 of 10
by Jesudas Chinnathampi (Das)
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 18353/ 696

How to add Modify/Edit rows in a ListView Web Server Control

In order for the user to edit a row in the ListView, they should be able to change the corresponding row in the ListView to an editable one. This can be done by adding the following to the ItemTemplate template to the ListView control.

Listing 9

<asp:LinkButton ID="lnkEdit" CommandName="Edit" runat="server" Text="Edit" />

The command name should be set to “Edit”. That is the most important aspect for the edit.

The second step for editing a ListView is to put the ListView in the edit mode. When the user clicks on any of the Edit Link, the corresponding row in the ListView should be changed to an editable row. This can be achieved using the following code snippet:

Listing 10

ListView1.EditIndex = e.NewEditIndex;

e is an instance of ListViewEditEventArgs.

Once the ListView is in the edit mode, we have two possibilities. The first being: Saving the edited data back to the database and the second is to cancel the edit operation. Canceling the edit operation can be done using the following code snippet:

Listing 11

ListView1.EditIndex = -1;

When the user clicks the Update link after editing the needed data, the event ItemUpdating will be fired. We could invoke a method for this event and within the method the modified data can be retrieved and update the database using a stored procedure or an inline SQL statement. Just like the insert operation, we could use the FindControl method to retrieve the modified values. Another key aspect for any edit operation is that we should know which row in the database should be updated. For this purpose, we could store the primary key for each row in the ListView inside a hidden label control or even a hidden server control.

Editing a DropDownList control within the edit mode of ListView

Editing a textbox control is very simple. But if you have a dropdown listbox, then pre-setting the old value for the dropdown might need some extra coding. There could be many ways you can pre-set the old value for the DropDownList control within the edit mode. One of the ways is as follows:

When the user clicks the Edit link for any row, get the value for the DropDownList control and store it in a global variable.

The event, ItemEditing will be fired when the edit link is clicked. In the ItemTemplate template for the ListView, the value for the DropDownList control is displayed as a label.

All we have to do is get the value of the label server control.

Using this label, we could change the index for the DropDownList control. The following code retrieves the old value for the DropDownList control (which is stored in a Label control)

Listing 12

ListView1.EditIndex = e.NewEditIndex;
// Get the Value for State
Label lblTemp = (Label)ListView1.Items[e.NewEditIndex].FindControl("lblState");
strCurrentState = lblTemp.Text;

Now using the string, strCurrentState we could change the index of the DropDownList control within the ItemDataBound event. The following method will be invoked during an ItemDataBound event occurs.

Listing 13

protected void DataBoundList(Object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    // Get a handle to the ddlState DropDownList control
    DropDownList ddl = (DropDownList)e.Item.FindControl("ddlState");
     // Make sure we have the handle !
    if (ddl != null)
    {
      ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText
        (strCurrentState));
    }
  }
}

Figure 3: ListView Edit Mode


View Entire Article

Article Feedback

Title:  
Name:  
Url: ( Optional )
Comment:  
Please add 8 and 6 and type the answer here:

User Comments

Title: YOU MUST TAKE THIS ARTICLE DOWN   
Name: HAN
Date: 6/20/2008 11:33:51 AM
Comment:
I spended quite a lot of hours and your codes are not working. I tried to make it work, but it just doesn't make sense and not working. I found it's better to put Updating, Inserting, and deleting on ItemCommand event. If you want to do all actions in code-behind, that's the best place you should embed the action codes. Your codes are too buggy and don't make sense in general. I hope anyone reading this as a beginner should avoid. Don't waste your time thinking you're doing something wrong. It's just the design of this codes are too nonsense
Title: Re: Go back and try again   
Name: Das (Author)
Date: 5/20/2008 9:05:08 PM
Comment:
This is fixed.

Well, this is a design flaw. When we have zero rows, the link to add authors was disabled. This is due to fact that the link to add new authors was in the ItemTemplate. When we have zero rows, the rows inside item template will not get rendered. So, I added an EmptyDataTemplate. Inside the EmptyDataTemplate, I have included a link to add authors. By this way, when someone deletes all rows, the link inside the EmptyDataTemplate will get rendered.
Title: Go back and try again   
Name: BugBuster
Date: 5/20/2008 1:19:20 PM
Comment:
Your code doesn't work, please correct it or remove from site. Tired of finding non-working examples.
Title: Editnig ListView   
Name: ListView
Date: 4/23/2008 3:08:32 AM
Comment:
Unable to edit
Title: Re: Live Demo is broken   
Name: Das
Date: 2/19/2008 6:13:48 PM
Comment:
It is a bug in my code. when the record count goes to zero, the link to "add new records" is not being displayed.

I need to fix this, so that the "Add new" link / button will always show up. so, when we have zero rows, we could add new row using the "Add New" functionality.

Will modify the code accordingly.
Title: Live Demo is broken   
Name: Jason Schechter
Date: 2/19/2008 2:38:06 PM
Comment:
Nothing is coming up except the shell of the table with the column headings.
Title: ListViewItemType   
Name: Wissam Bishouty
Date: 2/19/2008 6:33:06 AM
Comment:
Greetings,
thank you for the useful article.
in the DataBoundList method you say : if (ddl != null)
but why you do not check if the listview in edit mode so if it is in edit mode so you get the reference to the dropdownlist??

Regards.
Title: Re: Could not find stored procedure 'teacher.das_get_authors'.   
Name: Das
Date: 2/17/2008 6:39:19 PM
Comment:
The stored procedure got deleted somehow. Not sure how. But I just ran the create script again and the LIVE demo is working fine again.

Thanks for letting me know about the error
Title: Broken Demo   
Name: me
Date: 2/6/2008 4:43:35 PM
Comment:
Nice....
Could not find stored procedure 'teacher.das_get_authors'.

Product Spotlight
Product Spotlight 
Learn More
.NET Tools
asp.net shopping cart
asp.net chart control






Ads Powered by Lake Quincy Media
Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2008 ASPAlliance.com  |  Page Processed at 8/29/2008 7:54:45 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search