ListView中删除命令和ObjectDataSource删除方法无法正常工作 [英] ListView delete command and ObjectDataSource delete method not working correctly

查看:117
本文介绍了ListView中删除命令和ObjectDataSource删除方法无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ListView和一个ObjectDataSource。是ListView控件的属性如下:

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

正如人们所看到的,我设置的DataKeyNames。 id是我的书签表的分贝的主键。我使用LINQ to SQL的。该表的LINQ类名称为收藏。我的目标数据源的选择方法,工作正常,事情都出现在列表中。删除方法无法正常工作。

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.

我的表结构如下:

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

我已经建立了我的ObjectDataSource类,附加数据对象(真)我的课,我创建数据上下文对象,我的删除方法如下所示:

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();
}

在列表视图中,在我的链接按钮我必须要删除的命令,当我设置一个断点DeleteBookmark并点击链接按钮,破发点被激活,我就可以开始通过code步进。问题是,当我看着书签变量,没有被初始化,并且用户ID,这是一个GUID,看起来像这样:{} 00000000-0000-0000-0000-000000000000

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}.

问:我如何获得ListView控件和ObjectDataSource这样,当我点击与删除我的删除方法的参数是正确的初始化命令我的链接按钮,正常通信?我觉得我越来越近,但必须缺少的东西。有什么想法?

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?

我已经发现这篇文章展示了如何做到这一点,但它使用的GridView
并在除了编号上的DataKeyNames也有一个时间标记。是时间戳记需要,可在列表视图,为好,或者是具体到一个GridView?
<一href=\"http://weblogs.asp.net/zeeshanhirani/archive/2008/05/05/using-linq-to-sql-with-objectdatasource-control.aspx\"相对=nofollow>这里

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

推荐答案

在设计ObjectDataSource控件只是initalized键字段值的对象传递给删除方法。如果你希望所有的值传递,您必须设置 conflictdetection =CompareAllValues​​财产上的ObjectDataSource

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

这篇关于ListView中删除命令和ObjectDataSource删除方法无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆