ASP.NET ListView, custom DataSources, and editing items

Posted by Andrew Shepherd on Stack Overflow See other posts from Stack Overflow or by Andrew Shepherd
Published on 2009-02-23T23:36:58Z Indexed on 2010/06/01 22:33 UTC
Read the original article Hit count: 250

Filed under:
|
|

The MSDN walkthroughs provide a number of examples where you can drag a DataSource from the toolbox, run through some simple configuration steps, then drag a ListView onto the screen, point it at the DataSource, and hey - you've got full table editing.

Now I'm trying to write my own DataSource class (a class that implements System.Web.UI.IDataSource) and my own DataSourceView class. I now assign an instance of this custom DataSource class to the ListView.DataSource propery.

The display of all the items is working well. However, updating, inserting and deleting just is not working. I'm overriding every function I can in my DataSourceView class, and they just aren't being called.

This is such a huge topic, I'll focus this question on one simple example:

When you press the "Edit" button (the button inside the ItemTemplate with a CommandName of "Edit", you expect the ItemTemplate to be replaced by an EditItemTemplate. This did not happen. The only way I could get it to happen was to handle the onitemediting event.

protected void _listViewPublicHolidays_ItemEditing(object sender, ListViewEditEventArgs e)
{
    _listViewPublicHolidays.EditIndex = e.NewEditIndex;
    _listViewPublicHolidays.DataBind();
}

This is hardly a problem, but how come I had to do it at all? In the MSDN walkthroughs where I attach a ListView to a LinqDataSource, this code doesn't have to be written. Can someone who's been here before hazard a guess as to what would be different or missing in my custom datasource?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about ASP.NET