문제

세부 사항 컨트롤에 바인딩하는 ObjectDatasource가 있습니다. 비즈니스 계층 (데이터 계층으로 호출)에 삽입 방법이 작성되었으며 모든 것이 잘 작동합니다. 삽입 메소드가 발사되기 전에 다른 작업을 수행하고 싶을 때까지. 비즈니스 계층에 가기 전에 FileUpload 컨트롤에 액세스해야합니다. 그래서 나는 세부 사항 뷰에서 ItemCommand 이벤트를 연결했습니다. 이벤트를 선택하고 FileUpload 컨트롤을 사용하여 필요한 작업을 수행 할 수 있습니다. 이 경우 비즈니스 계층의 삽입 메소드를 호출합니다. 그러나 삽입 방법이 두 번 발생합니다! 이것에 대해 1 분 동안 생각한 후 나는 이것이 예상되는 행동임을 깨달았습니다. ItemCommand 이벤트에서 호출 될 때 한 번 발생하고 ObjectDatasource InsertMethod에서 두 번째로 발사되었습니다.

나는 그 방법의 이중 불을 제거하기 위해 ObjectDatasource에서 insertmethod 속성을 간단히 제거 할 수 있다고 생각했지만, 그렇게하면이 오류가 발생합니다.

삽입은 삽입물이 지정되지 않는 한 ObjectDatasource 'objstudentDetails'에 의해 지원되지 않습니다.

그렇다면 대상 Datasource에게 방법을 발사하지 말라고 말할 수있는 방법이 있습니까? 아래 코드 단순화 코드를 참조하십시오.

<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에서 삽입 이벤트를 처리 할 수없는 이유가 있습니까? 원하는 경우 인서트를 취소하는 방법도 있습니다.

Markup의 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;    
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top