DetailsViewUpdateEventArgs クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説DetailsView コントロールは、コントロール内の Update ボタン (CommandName プロパティが "Update" に設定されたボタン) がクリックされた場合に、DetailsView コントロールが実際にレコードを更新する前に ItemUpdating イベントを発生させます。これにより、このイベントが発生するたびにカスタム ルーチン (データ ソースでレコードを更新する前にレコードの値を HTML エンコーディングするなど) を実行するイベント ハンドラを提供できます。
DetailsViewUpdateEventArgs オブジェクトがイベント ハンドラに渡されることにより、DetailsView コントロールに送信される省略可能なコマンド引数の値を確認したり、更新操作をキャンセルする必要があるかどうかを示したりできます。コマンド引数の値を確認するには、CommandArgument プロパティを使用します。更新操作をキャンセルするには、Cancel プロパティを true に設定します。また、Keys プロパティおよび NewValues プロパティを使用して、ユーザーが入力した新しい値を読み込んだり、変更したりすることもできます。Keys プロパティにはキー フィールドが格納され、NewValues プロパティにはキー以外のフィールドが格納されます。元のキー フィールド以外の値にアクセスする必要がある場合は、OldValues プロパティを使用します。
イベント処理の詳細については、「イベントの利用」を参照してください。
DetailsViewUpdateEventArgs クラスのインスタンスの初期プロパティ値の一覧については、DetailsViewUpdateEventArgs コンストラクタのトピックを参照してください。
使用例ItemUpdating イベントのイベント ハンドラに渡された DetailsViewUpdateEventArgs オブジェクトを使用して、ユーザーが入力した値を検証する方法のコード例を次に示します。
<%@ Page language="VB" %> <script runat="server"> Sub CustomerDetailsView_ItemUpdating(ByVal sender As Object, _ ByVal e As DetailsViewUpdateEventArgs) ' Validate the field values entered by the user. This ' example determines whether the user left any fields ' empty. Use the NewValues property to access the new ' values entered by the user. Dim emptyFieldList As ArrayList = _ ValidateFields(CType(e.NewValues, IOrderedDictionary)) If emptyFieldList.Count > 0 Then ' The user left some fields empty. Display an error message. ' Use the Keys property to retrieve the key field value. Dim keyValue As String = e.Keys("CustomerID").ToString() MessageLabel.Text = _ "You must enter a value for all fields of record " & _ keyValue & ".<br/>The following fields are missing:<br/><br/>" ' Display the missing fields. Dim value As String For Each value In emptyFieldList ' Use the OldValues property access the original value ' of a field. MessageLabel.Text &= value & " - Original Value = " & _ e.OldValues(value).ToString() & "<br>" Next ' Cancel the update operation. e.Cancel = True Else ' The field values passed validation. Clear the ' error message label. MessageLabel.Text = "" End If End Sub Function ValidateFields(ByVal list As IOrderedDictionary) _ As ArrayList ' Create an ArrayList object to store the ' names of any empty fields. Dim emptyFieldList As New ArrayList() ' Iterate though the field values entered by ' the user and check for an empty field. Empty ' fields contain a null value. Dim entry As DictionaryEntry For Each entry In list If entry.Value Is Nothing Then ' Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()) End If Next Return emptyFieldList End Function Sub CustomerDetailsView_ModeChanging(ByVal sender As Object, ByVal e As DetailsViewModeEventArgs) If e.CancelingEdit Then ' The user canceled the update operation. ' Clear the error message label. MessageLabel.Text = "" End If End Sub </script> <html> <body> <form runat="server"> <h3>DetailsViewUpdateEventArgs Example</h3> <asp:detailsview id="CustomerDetailsView" datasourceid="DetailsViewSource" autogeneraterows="true" autogenerateeditbutton="true" allowpaging="true" datakeynames="CustomerID" onitemupdating="CustomerDetailsView_ItemUpdating" onmodechanging="CustomerDetailsView_ModeChanging" runat="server"> <pagersettings position="Bottom"/> </asp:detailsview> <br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update [Customers] Set [CompanyName]=@CompanyName, [Address]=@Address, [City]=@City, [PostalCode]=@PostalCode, [Country]=@Country Where [CustomerID]=@CustomerID" connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void CustomerDetailsView_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e) { // Validate the field values entered by the user. This // example determines whether the user left any fields // empty. Use the NewValues property to access the new // values entered by the user. ArrayList emptyFieldList = ValidateFields((IOrderedDictionary)e.NewValues); if (emptyFieldList.Count > 0) { // The user left some fields empty. // Display an error message. // Use the Keys property to retrieve the key field value. String keyValue = e.Keys["CustomerID"].ToString(); MessageLabel.Text = "You must enter a value for all fields of record " + keyValue + ".<br/>The following fields are missing:<br/><br/>"; // Display the missing fields. foreach (String value in emptyFieldList) { // Use the OldValues property access the original // value of a field. MessageLabel.Text += value + " - Original Value = " + e.OldValues[value].ToString() + "<br>"; } // Cancel the update operation. e.Cancel = true; } else { // The field values passed validation. Clear the // error message label. MessageLabel.Text = ""; } } ArrayList ValidateFields(IOrderedDictionary list) { // Create an ArrayList object to store the // names of any empty fields. ArrayList emptyFieldList = new ArrayList(); // Iterate though the field values entered by // the user and check for an empty field. Empty // fields contain a null value. foreach (DictionaryEntry entry in list) { if (entry.Value == null) { // Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()); } } return emptyFieldList; } void CustomerDetailsView_ModeChanging(Object sender, DetailsViewModeEventArgs e) { if (e.CancelingEdit) { // The user canceled the update operation. // Clear the error message label. MessageLabel.Text = ""; } } </script> <html> <body> <form runat="server"> <h3>DetailsViewUpdateEventArgs Example</h3> <asp:detailsview id="CustomerDetailsView" datasourceid="DetailsViewSource" autogeneraterows="true" autogenerateeditbutton="true" allowpaging="true" datakeynames="CustomerID" onitemupdating="CustomerDetailsView_ItemUpdating" onmodechanging="CustomerDetailsView_ModeChanging" runat="server"> <pagersettings position="Bottom"/> </asp:detailsview> <br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update [Customers] Set [CompanyName]=@CompanyName, [Address]=@Address, [City]=@City, [PostalCode]=@PostalCode, [Country]=@Country Where [CustomerID]=@CustomerID" connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
.NET Framework のセキュリティ
継承階層System.EventArgs
System.ComponentModel.CancelEventArgs
System.Web.UI.WebControls.DetailsViewUpdateEventArgs
スレッド セーフ
プラットフォームWindows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照DetailsViewUpdateEventArgs コンストラクタ
アセンブリ: System.Web (system.web.dll 内)
構文
解説このコンストラクタを使用して、DetailsViewUpdateEventArgs クラスの新しいインスタンスを初期化します。
DetailsViewUpdateEventArgs クラスのインスタンスの初期プロパティ値を次の表に示します。
メモ |
|---|
プラットフォームWindows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照DetailsViewUpdateEventArgs プロパティ
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Cancel | イベントをキャンセルするかどうかを示す値を取得または設定します。 ( CancelEventArgs から継承されます。) |
| CommandArgument | DetailsView コントロールに渡される、更新処理のコマンド引数を取得します。 |
| Keys | 更新するレコードのキー フィールドの名前/値ペアを格納しているディクショナリを取得します。 |
| NewValues | 更新するレコードの新しいフィールドの名前/値ペアを格納しているディクショナリを取得します。 |
| OldValues | 更新するレコードの元のフィールドの名前/値ペアを格納しているディクショナリを取得します。 |
参照DetailsViewUpdateEventArgs メソッド
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
参照DetailsViewUpdateEventArgs メンバ
DetailsViewUpdateEventArgs データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック プロパティ| 名前 | 説明 | |
|---|---|---|
| Cancel | イベントをキャンセルするかどうかを示す値を取得または設定します。(CancelEventArgs から継承されます。) |
| CommandArgument | DetailsView コントロールに渡される、更新処理のコマンド引数を取得します。 |
| Keys | 更新するレコードのキー フィールドの名前/値ペアを格納しているディクショナリを取得します。 |
| NewValues | 更新するレコードの新しいフィールドの名前/値ペアを格納しているディクショナリを取得します。 |
| OldValues | 更新するレコードの元のフィールドの名前/値ペアを格納しているディクショナリを取得します。 |
パブリック メソッド| 名前 | 説明 | |
|---|---|---|
| Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
| GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
| GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
| ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
| ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
プロテクト メソッド| 名前 | 説明 | |
|---|---|---|
| Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
| MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
参照- DetailsViewUpdateEventArgsのページへのリンク
.gif)