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 met开发者_如何学Gohod 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
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
Its because you did not add the Delete parameters
to your ObjectDataSource
.
<DeleteParameters>
<asp:Parameter Name="" Type="" />
</DeleteParameters>
精彩评论