views:

1531

answers:

2

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?

+1  A: 

I know this is an alternate approach and may not work in your specific case, but you may want to look at using the ObjectDataSource with a simple object model and it will do some of the work for you, rather than writing a whole DataSource.

So for example you could create a fairly simple PublicHolidays class with a couple methods for

public List<PublicHoliday> GetAllPublicHolidays()

and

public void UpdatePublicHoliday(string name, date holidayDate)

etc, and you can point the ObjectDataSource straight to those methods.

routeNpingme
I've experimented with ObjectDataSource, but the problem I couldn't get around was initialisation. In this example, the ObjectDataSource would create a PublicHolidays class just before selection, then destroy it again. What if I wanted to filter it based on a value in a control on the page?
Andrew Shepherd
In your GetAllPublicHolidays() method, you can include parameters that you fill in at runtime to filter the results, or some of the actual grid controls can do the filtering for you too...
routeNpingme
A: 

Using a LinqDataSource or ObjectDataSource, the binding, setting the editindex etc is handled through the data source. If you bind to your own collection or dataset you need to do a little plumbing yourself. Those datasource classes handle maintaining state of the data automatically, where standard collections such as lists or datastsets don't.

Jeremy