grdview_RowDeleting DataItem 为空
我已将数据网格绑定到列表。我遵循存储库模式并使用 EF v4.1。我需要删除 row_deleting 事件上的实体。这是代码:
protected void grdBooks_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int bookId = (int)e.Keys[0];
//grdBooks.Rows[e.RowIndex] //this item's dataItem is always null.
}
当我使用实体时,我需要实际的实体将其传递到我的 GenericRepository,这将删除该实体。我确实获得了 bookId,但我不想做一些愚蠢的事情,例如使用此 bookId 从数据库中获取实体,然后将其传递给我的删除方法。为什么 DataItem 始终为空,我该怎么做才能取回我的实体?
I have bound my datagrid to a list. I am following Repository pattern and using EF v4.1. I need to delete the Entity on row_deleting event. This is the code:
protected void grdBooks_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{
int bookId = (int)e.Keys[0];
//grdBooks.Rows[e.RowIndex] //this item's dataItem is always null.
}
As I am working with Entities, I need the actual Entity to pass it to my GenericRepository which will delete that entity. I did get the bookId but I don't want to do silly things like fetching the Entity from database using this bookId and then passing it to my delete method. Why is the DataItem always null and what can I do to get back my entity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DataItem = null 因为当您在 Page_Load 中将数据绑定到 gridview 一次时,
数据将存储在 Viewstate 中。单击删除后,您将回发到服务器。数据仍然存在于 Gridview 的视图状态中并且不会反弹。
如果您有 Id,则可以删除该对象。您可以通过 3 种不同的方式来完成此操作
http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retriving-it。 aspx
调用 context.ExecuteStoreCommand 并执行常规 DELETE FROM -http://msdn.microsoft.com/en-us/library/ee358769.aspx(第二个示例)
在数据库上创建存储过程,将其导入模型并使用上下文调用它。 ExecuteFunction()
DataItem = null because when You databind data to gridview only once in Page_Load
data is stored in Viewstate. After You click delete You postback to server. Data is still present in Gridview's viewstate and not rebound.
You can delete the object if You have Id. You can do it in 3 different ways
http://blogs.msdn.com/b/alexj/archive/2009/03/27/tip-9-deleting-an-object-without-retrieving-it.aspx
call context.ExecuteStoreCommand and just do the regular DELETE FROM -http://msdn.microsoft.com/en-us/library/ee358769.aspx (2nd example)
create stored procedure on database, import it to model and the call it using context.ExecuteFunction()
您不需要获取任何内容即可将其删除。只需创建具有指定 ID 的实体,附加它,然后调用删除。
You do not need fetch anything to delete it. Just create Entity with specified Id, Attach it and then call Remove.
我不确定我是否正确理解了你的问题,但我认为你应该尝试这个
I am not suer whether I have understood your question right or not but I think you should try this