ObjectDataSource控件插入法
-
12-09-2019 - |
题
我有我结合DetailsView控件一个ObjectDataSource。我已经写在业务层插入方法(它调用分解成一个数据层),一切工作正常..直到我想要做的插入方法触发之前别的东西。去之前我的业务层,我需要访问FileUpload控件。所以,我线连上DetailsView控件的ItemCommand事件 - 它拿起时,我可以做我需要与FileUpload控件就好了。在这种情况下我呼吁在业务层插入方法 - 在ObjectDataSource控件中指定的方法相同。但是Insert方法闪光两次!在这个思想一分钟后,我意识到这是预期的行为 - 从ItemCommand事件调用时,它的发射一次,从ObjectDataSource控件InsertMethod第二次。
我想我可以简单地从ObjectDataSource控件删除InsertMethod属性来消除该方法的双火,但是当我这样做,我得到这个错误:
插入不被支持 ObjectDataSource控件 '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..
}
解决方案
是否有你不能只是处理的ObjectDataSource控件插入事件的原因吗?它甚至有一个方法来取消插入,如果你想要的。
就在事件处理程序添加到ObjectDataSource在标记(或使用设计器):
<asp:ObjectDataSource id=CustomerObjectDataSource" runat="server"
oninserting="CustomerObjectDataSource_Inserting"
</asp:ObjectDataSource>
此事件只是插入之前,如果你需要从传播阻止它,你可以做这样的事情:
protected void CustomerObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
InsertMethod(someParams);
//If you are satisfied with what has already been done..
e.Cancel = true;
}
不隶属于 StackOverflow