2

DetailsView コントロールにバインドしている ObjectDataSource があります。ビジネス レイヤー (データ レイヤーに呼び出します) に挿入メソッドを記述しましたが、挿入メソッドが起動する前に何か他のことをしたいまで、すべて正常に動作します。ビジネス レイヤーに移動する前に、ファイル アップロード コントロールにアクセスする必要があります。そこで、DetailsView に ItemCommand イベントを接続しました。イベントがピックアップされ、必要なことを FileUpload コントロールでうまく実行できます。その場合、ビジネス層で挿入メソッドを呼び出します。これは、ObjectDataSource コントロールで指定されたメソッドと同じです。しかし、Insert メソッドは 2 回起動します。これについて少し考えた後、これが予想される動作であることがわかりました.ItemCommandイベントから呼び出されたときに1回、ObjectDataSource InsertMethodから2回目が呼び出されます。

ObjectDataSource から InsertMethod 属性を単純に削除して、そのメソッドの二重発火をなくすことができると考えましたが、そうすると次のエラーが発生します。

InsertMethod が指定されていない限り、挿入は ObjectDataSource 'objStudentDetails' によってサポートされていません。

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..
}
4

1 に答える 1

5

ObjectDataSource で Inserting イベントを処理できない理由はありますか? 必要に応じて、挿入をキャンセルする方法もあります。

マークアップで 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;    
}
于 2009-06-08T01:29:45.800 に答える