ListView 删除命令和 ObjectDataSource 删除方法无法正常工作

发布于 2024-11-19 16:09:28 字数 2719 浏览 0 评论 0原文

我正在使用 ListView 和 ObjectDataSource。 ListView 的属性如下:

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

可以看到,我已经设置了 DataKeyNames。 Id 是数据库中书签表的主键。我正在使用 linq to sql。该表的 linq 类名称是 Bookmark。我的对象数据源上的选择方法工作正常,并且内容显示在列表中。删除方法无法正常工作。

我的表结构如下:

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];

下面,我列出了我声明的对象数据源:

<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" />

我创建了我的 ObjectDataSource 类,将 DataObject(true) 附加到我的类,创建了我的数据上下文对象,我的删除方法如下所示:

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

在列表视图中,在我的链接按钮上,我将命令设置为“删除”,当我将断点设置为“DeleteBookmark”并单击链接按钮时,断点被激活,我可以开始单步执行该代码。问题是,当我查看书签变量时,没有任何内容被初始化,而 UserId(一个 guid)如下所示:{00000000-0000-0000-0000-000000000000}。

问题:如何使列表视图和对象数据源正确通信,以便当我使用删除命令单击链接按钮时,删除方法的参数能够正确初始化?我想我已经很接近了,但一定错过了一些东西。有什么想法吗?

我发现这篇文章展示了如何执行此操作,但它使用 GridView 并且除了Id之外,DataKeyNames上还有一个时间戳。列表视图中是否也需要时间戳,或者是特定于 GridView 的时间戳? 此处

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2024-11-26 16:09:28

根据设计,ObjectDataSource 只是将对象中的关键字段值初始化并传递给删除方法。如果您希望传递所有值,则必须在 ObjectDataSource 上设置 conflictdetection="CompareAllValues" 属性

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

囍孤女 2024-11-26 16:09:28

这是因为您没有将 Delete 参数 添加到 ObjectDataSource 中。

 <DeleteParameters>
        <asp:Parameter Name="" Type="" />
  </DeleteParameters>

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

 <DeleteParameters>
        <asp:Parameter Name="" Type="" />
  </DeleteParameters>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文