1

I am using a ListView and an ObjectDataSource. The attributes of the ListView are as follows:

<asp:ListView ID="BookmarkManagerListView"
              DataKeyNames="Id"
              DataSourceID="BookmarkManager_Default_ObjectDataSource"
              ItemPlaceholderID="ItemPlaceHolder"
              OnItemDataBound="BookmarkManagerListView_ItemDataBound" 
              runat="server">
    ...

As one can see, I've set the DataKeyNames. Id is the primary key of my Bookmarks table in the db. I am using linq to sql. The linq class name for the table is Bookmark. The select method on my object data source works correctly and things are showing up in the list. The delete method is not working correctly.

My table structure is as follows:

CREATE TABLE [dbo].[Bookmarks](
  [Id] [int] IDENTITY(1,1) NOT NULL,
  [CategoryId] [int] NULL,
  [TypeId] [int] NOT NULL,
  [UserId] [uniqueidentifier] NOT NULL,
  [Title] [varchar](200) NOT NULL,
  [Url] [varchar](1500) NOT NULL,
  CONSTRAINT [PK_Bookmarks] PRIMARY KEY CLUSTERED
  (
     [Id] ASC
  ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY];

Below, I'm listing my declared object data source:

<asp:ObjectDataSource ID="BookmarkManager_Default_ObjectDataSource" 
                      DataObjectTypeNames="AppName.Model.Bookmark"
                      SelectMethod="SelectAll"
                      SelectCountMethod="GetSelectCount"
                      DeleteMethod="DeleteBookmark"
                      TypeName="AppName.WebApp.UserControls.Bookmark.BookmarkManagerObjectDataSource"
                      OnSelecting="ObjectDataSource_Default_Selecting"
                      OldValuesParameterFormatString="original_{0}"
                      SortParameterName="sortType"
                      EnablePaging="false"
                      StartRowIndexParameterName="startRowIndex"
                      MaximumRowsParameterName="maximumRows"
                      runat="server" />

I've created my ObjectDataSource class, appended DataObject(true) to my class, created my data context object, and my delete method looks like the following:

[DataObjectMethod(DataObjectMethodType.Delete, true)]
public void DeleteBookmark(Model.Bookmark bookmark)
{
    _dc.Bookmarks.Attach(bookmark);
    _dc.Bookmarks.DeleteOnSubmit(bookmark);
    _dc.SubmitChanges();
}

In the listview, on my link button I have the command set to Delete and when I set a break point to DeleteBookmark and click the link button, break point is activated and I can begin stepping through that code. Problem is when I look at the bookmark variable, nothing is initialized, and UserId, which is a guid, looks like this: {00000000-0000-0000-0000-000000000000}.

Question: How do I get the listview and objectdatasource to communicate correctly so that when I click my link button with command of delete that the parameter for my delete method is initialized correctly? I think I'm getting close but must be missing something. Any thoughts?

I have found this article which shows how to do this, but its using a GridView and in addition to the Id on the DataKeyNames also has a timestamp. Is a timestamp needed, in a listview, as well, or is that specific to a GridView? here

2 Answers2

1

By design ObjectDataSource just initalized key fields value in the object pass to delete method. If you want all the values to be passed, you must set conflictdetection="CompareAllValues" property on the ObjectDataSource

Duck
  • 11
  • 1
0

Its because you did not add the Delete parameters to your ObjectDataSource.

 <DeleteParameters>
        <asp:Parameter Name="" Type="" />
  </DeleteParameters>
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • 1
    actually, I don't believe that is it. Because I have added DataObjectTypeName to my ObjectDataSource, the delete parameters wont be needed. Actually, I just got it to work. In my ListView on attribute DataKeyNames, i've chnaged to "Id,CategoryId,TypeId,UserId,Title,Url" and now the delete works. If I hadn't specified DataObjectTypeName and no DeleteParameters then I would have received exception saying that ObjectDataSource con't find delete method with no parameters. Well, that had actually happend and came accross a website saying to use DataObjectTypeName. –  Jul 12 '11 at 15:13
  • its weird that I have to specify all my extra columns in the DataKeyNames but if it works it works. –  Jul 12 '11 at 15:14
  • Is there a reason why this would work now with the extra stuff specified in the DataKeyNames? –  Jul 12 '11 at 15:14
  • as it turns out need to add to DataKeyNames if one wants specific properties on your object to be populated. I did the following: DataKeyNames="Id,UserId,BookmarkTypeId,..." and all worked. –  Jul 17 '11 at 19:54
  • 1
    @developerdoug: You should turn your discovery of setting the ListView's DataKeyNames into an answer, so I can vote it up. It helped me in any case! – Fedor Alexander Steeman Apr 26 '12 at 12:40