ObjectDataSource插入方法
我有一个 ObjectDataSource,我将其绑定到 DetailsView 控件。 我在业务层中编写了插入方法(它向下调用数据层)并且一切正常......直到我想在插入方法触发之前执行其他操作。 在进入业务层之前,我需要访问文件上传控件。 因此,我在 DetailsView 上连接了一个 ItemCommand 事件 - 它拾取该事件,我可以使用 FileUpload 控件完成我需要的操作。 在这种情况下,我调用业务层中的插入方法 - 与 ObjectDataSource 控件中指定的方法相同。 但是 Insert 方法会触发两次! 思考了一分钟后,我意识到这是预期的行为 - 从 ItemCommand 事件调用时触发一次,从 ObjectDataSource InsertMethod 调用时触发第二次。
我以为我可以简单地从 ObjectDataSource 中删除 InsertMethod 属性来消除该方法的双重触发,但当我这样做时,我收到此错误:
不支持插入 对象数据源“objStudentDetails” 除非指定了 InsertMethod。
那么有什么方法可以告诉 ObjectDataSource 不要触发该方法吗? 代码见简化代码如下:
<asp:DetailsView ID="dtvStudentDetails"
runat="server"
AutoGenerateRows="False"
DataSourceID="objStudentDetails"
OnItemCommand="dtvStudentDetails_ItemCommand">
:
:
</asp:DetailsView>
<asp:ObjectDataSource ID="objStudentDetails"
runat="server"
TypeName="AIMLibrary.BLL.Students"
SelectMethod="GetStudentDetails"
UpdateMethod="UpdateStudent">
:
:
</asp:ObjectDataSource>
public static Int32 InsertStudent(Int32 studentId, String firstName, String lastName, String employer, String phone, String email, String address, String city, String state, String zip, String dob, String cardImagePath)
{
StudentDetails record = new StudentDetails(firstName, lastName, employer, phone, email, address, city, state, zip, dob, cardImagePath);
StudentsProvider provider = new StudentsProvider();
return provider.InsertStudent(record); //actual insert happens in here..
}
I have an ObjectDataSource that I'm binding to a DetailsView control. I have the insert method written in a business layer (which calls down into a data layer) and everything works fine.. until i want to do something else before the insert method fires. Before going to my business layer I need access to a fileupload control. So I wired up an ItemCommand event on the DetailsView - it picks up the event and I can do what i need with the FileUpload control just fine. In that event I call the insert method in the business layer - the same method specified in the ObjectDataSource control. But the Insert method fires twice! After thinking on this for a minute i realize this is the expected behavior - it's fired once when called from the ItemCommand event, and the second time from ObjectDataSource InsertMethod.
I thought I could simply remove the InsertMethod attribute from the ObjectDataSource to eliminate the double fire on that method, but when I do that I get this error:
Inserting is not supported by
ObjectDataSource 'objStudentDetails'
unless the InsertMethod is specified.
So is there any way I can tell the ObjectDataSource not to fire the method? See code simplified code below:
<asp:DetailsView ID="dtvStudentDetails"
runat="server"
AutoGenerateRows="False"
DataSourceID="objStudentDetails"
OnItemCommand="dtvStudentDetails_ItemCommand">
:
:
</asp:DetailsView>
<asp:ObjectDataSource ID="objStudentDetails"
runat="server"
TypeName="AIMLibrary.BLL.Students"
SelectMethod="GetStudentDetails"
UpdateMethod="UpdateStudent">
:
:
</asp:ObjectDataSource>
public static Int32 InsertStudent(Int32 studentId, String firstName, String lastName, String employer, String phone, String email, String address, String city, String state, String zip, String dob, String cardImagePath)
{
StudentDetails record = new StudentDetails(firstName, lastName, employer, phone, email, address, city, state, zip, dob, cardImagePath);
StudentsProvider provider = new StudentsProvider();
return provider.InsertStudent(record); //actual insert happens in here..
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是否有理由不能只处理 ObjectDataSource 上的 Inserting 事件? 如果需要的话,它甚至可以取消插入。
只需将事件处理程序添加到标记中的 ObjectDataSource(或使用设计器):
此事件在插入之前触发,如果您需要阻止它传播,您可以执行以下操作:
Is there a reason you can't just handle the Inserting event on the ObjectDataSource? It even has a way to cancel the insert if you want.
Just add the event handler to the ObjectDataSource in markup (or use the designer):
This event fires just before the insert, and if you need to stop it from propagating, you can do something like this: