使用 FormView/ObjectDataSource 控件插入后如何获取插入的 id(或对象)?
我有一系列松散地符合以下模式的类:
public class CustomerInfo {
public int Id {get; set;}
public string Name {get; set;}
}
public class CustomerTable {
public bool Insert(CustomerInfo info) { /*...*/ }
public bool Update(CustomerInfo info) { /*...*/ }
public CustomerInfo Get(int id) { /*...*/ }
/*...*/
}
成功插入后,Insert 方法将设置传入的 CustomerInfo 对象的 Id 属性。我已经在多个地方使用了这些类,并且更愿意更改它们。 现在我正在尝试编写一个 ASP.NET 页面来插入和更新记录。 我当前正在使用 ObjectDataSource 和 FormView 控件:
<asp:ObjectDataSource
TypeName="CustomerTable"
DataObjectTypeName="CustomerInfo"
InsertMethod="Insert"
UpdateMethod="Update"
SelectMethod="Get"
/>
我可以成功插入和更新记录。我想在成功插入后将 FormView 的模式从插入切换为编辑。 我的第一次尝试是在 ItemInserted 事件中切换模式。
这当然行不通。我使用 QueryStringParameter 作为 id,当然在插入时不会设置它。
因此,我转而在 ObjectDataSource 的选择事件期间手动填充输入参数。问题是我需要知道新插入的记录的 id,但我找不到一个好的方法来获取。
我知道我可以访问 Insert 方法的返回值,以及 ItemInserted 事件中的 out 参数,当然我的方法不会使用这些方法中的任何一个返回 id。无论如何,我找不到访问插入完成后插入的 id 或 CustomerInfo 对象的方法。
我能做的最好的事情就是将 CustomerInfo 对象保存在 ObjectDataSource 的 Inserting 事件中。
这感觉是一种奇怪的方法。我想一定有更好的方法来做到这一点,当我看到它时我会踢自己的。有什么想法吗?
I have a series of classes that loosely fit the following pattern:
public class CustomerInfo {
public int Id {get; set;}
public string Name {get; set;}
}
public class CustomerTable {
public bool Insert(CustomerInfo info) { /*...*/ }
public bool Update(CustomerInfo info) { /*...*/ }
public CustomerInfo Get(int id) { /*...*/ }
/*...*/
}
After a successful insert the Insert method will set the Id property of the CustomerInfo object that was passed in. I've used these classes in several places and would prefer to altering them.
Now I'm experimenting with writing an ASP.NET page for inserting and updating the records.
I'm currently using the ObjectDataSource and FormView controls:
<asp:ObjectDataSource
TypeName="CustomerTable"
DataObjectTypeName="CustomerInfo"
InsertMethod="Insert"
UpdateMethod="Update"
SelectMethod="Get"
/>
I can successfully Insert and Update records. I would like to switch the FormView's mode from Insert to Edit after a successful insert.
My first attempt was to switch the mode in the ItemInserted event.
This of course did not work. I was using a QueryStringParameter for the id which of course wan't set when inserting.
So, I switched to manually populating the InputParameters during the ObjectDataSource's Selecting event. The problem with this is I need to know the id of the newly inserted record which I can't find a good way to get.
I understand that I can access the Insert method's return value, and out parameters in the ItemInserted event of course my method doesn't return the id using any of these methods. I can't find anyway to access the id or the CustomerInfo object that was inserted after the insert completes.
The best I've been able to do is to save the CustomerInfo object in the ObjectDataSource's Inserting event.
This feels like an odd way to do this. I figure that there must be a better way to do this and I'll kick myself when I see it. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果需要检索对象数据源上调用的插入方法的返回值,请使用“Inserted”事件,如下所示:
无需参数即可捕获返回值。对于更新方法,请使用“已更新”
If you need to retrieve the return value of the invoked insert method on a objectdatasource, use the “Inserted” event like so:
No parameter needed to catch the return value. For the Update method use the “Updated”