DynamicData を使用して、新しい一意の行を挿入するときに完全に正常に機能する標準の多対多テーブルを作成しました。両方の親テーブルのドロップダウン リストが適切に表示され、行が正しく挿入されます。
ただし、1 つのテーブルには、OnInserted イベント ハンドラーでキャッチできる例外をスローする Unique Index があります。次に、問題を知らせるフレンドリーなメッセージをユーザーに返信できます。この問題は、DetailsView がビューステートを失い、ユーザーが DropDownLists に対して選択した値がページのデフォルトにリセットされるために発生します。
protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
{
if (e.Exception != null && !e.ExceptionHandled)
{
msgPanel.Text = "Error Occurred"; // or some other friendly message
e.ExceptionHandled = true;
// e.Result == null, so I can't rebuild the user's input from here.
// What can be put where to get the user's input restored?
}
}