GridViewDeleteEventArgs.Keys プロパティ
アセンブリ: System.Web (system.web.dll 内)

削除する行の主キーを表すフィールドの名前と値のペアが格納された System.Collections.Specialized.IOrderedDictionary オブジェクト。

GridView コントロールの DataKeyNames プロパティが設定されている場合、Keys プロパティ (ディクショナリ) を使用して、行の削除する 1 つ以上の主キーの値にアクセスします。
![]() |
---|
Keys ディクショナリには、DataKeyNames プロパティで指定された 1 つ以上のフィールドの名前と値のペアが自動的に格納されます。主キーが複数のフィールドで構成されている場合、キー フィールドごとに別のエントリが Keys ディクショナリに追加されます。
キー フィールドの名前を確認するには、Keys ディクショナリに格納されている System.Collections.DictionaryEntry オブジェクトの DictionaryEntry.Key プロパティを使用します。キー フィールドの値を確認するには、DictionaryEntry.Value プロパティを使用します。

Values プロパティを使用して、削除する行のキー フィールドの値にアクセスする方法を次の例に示します。その後、削除されたレコードのログ ファイルに値が書き込まれます。
<%@ Page language="VB" %> <%@ import namespace="System.IO" %> <script runat="server"> Sub CustomersGridView_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) ' Record the delete operation in a log file. ' Create the log text. Dim logText As String = "" ' Append the values of the key fields to the log text. Dim i As Integer For i = 0 To e.Keys.Count - 1 logText &= e.Keys(i).ToString() & ";" Next ' Append the values of the non-key fields to the log text. For i = 0 To e.Values.Count - 1 If e.Values(i) IsNot Nothing Then logText &= e.Values(i).ToString() & ";" Else logText &= "Nothing" & ";" End If Next ' Display the log content. LogTextLabel.Text = logText ' Append the text to a log file. Try Dim sw As StreamWriter sw = File.AppendText(Server.MapPath(Nothing) & "\deletelog.txt") sw.WriteLine(logText) sw.Flush() sw.Close() Catch ex As UnauthorizedAccessException ' You must provide read/write access to the file using ACLs. LogErrorLabel.Text = "You do not have permission to write to the log." End Try End Sub Sub CustomersGridView_RowDeleted(ByVal sender As Object, ByVal e As GridViewDeletedEventArgs) If e.Exception Is Nothing Then ' The delete operation succeeded. Clear the message label. Message.Text = "" Else ' The delete operation failed. Display an error message. Message.Text = e.AffectedRows.ToString() & " rows deleted. " & e.Exception.Message e.ExceptionHandled = True End If End Sub </script> <html> <body> <form runat="server"> <h3>GridViewDeleteEventArgs Keys and Values Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <br/> <asp:label id="LogTextLabel" forecolor="Red" runat="server"/> <br/> <asp:label id="LogErrorLabel" forecolor="Red" runat="server"/> <br/> <asp:gridview id="CustomersGridView" allowpaging="true" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" datakeynames="CustomerID" onrowdeleted="CustomersGridView_RowDeleted" onrowdeleting="CustomersGridView_RowDeleting" runat="server"> </asp:gridview> <!-- 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="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <%@ import namespace="System.IO" %> <script runat="server"> void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e) { // Record the delete operation in a log file. // Create the log text. String logText = ""; // Append the values of the key fields to the log text. foreach (DictionaryEntry keyEntry in e.Keys) { logText += keyEntry.Key + "=" + keyEntry.Value + ";"; } // Append the values of the non-key fields to the log text. foreach (DictionaryEntry valueEntry in e.Values) { logText += valueEntry.Key + "=" + valueEntry.Value + ";"; } // Display the log content. LogTextLabel.Text = logText; // Append the text to a log file. try { StreamWriter sw; sw = File.AppendText(Server.MapPath(null) + "\\deletelog.txt"); sw.WriteLine(logText); sw.Flush(); sw.Close(); } catch(UnauthorizedAccessException ex) { // You must provide read/write access to the file using ACLs. LogErrorLabel.Text = "You do not have permission to write to the log."; } } void CustomersGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e) { if (e.Exception == null) { // The delete operation succeeded. Clear the message label. Message.Text = ""; } else { // The delete operation failed. Display an error message. Message.Text = e.AffectedRows.ToString() + " rows deleted. " + e.Exception.Message; e.ExceptionHandled = true; } } </script> <html> <body> <form runat="server"> <h3>GridViewDeleteEventArgs Keys and Values Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <br/> <asp:label id="LogTextLabel" forecolor="Red" runat="server"/> <br/> <asp:label id="LogErrorLabel" forecolor="Red" runat="server"/> <br/> <asp:gridview id="CustomersGridView" allowpaging="true" datasourceid="CustomersSqlDataSource" autogeneratecolumns="true" autogeneratedeletebutton="true" datakeynames="CustomerID" onrowdeleted="CustomersGridView_RowDeleted" onrowdeleting="CustomersGridView_RowDeleting" runat="server"> </asp:gridview> <!-- 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="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" deletecommand="Delete from Customers where CustomerID = @CustomerID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からGridViewDeleteEventArgs.Keys プロパティを検索する場合は、下記のリンクをクリックしてください。

- GridViewDeleteEventArgs.Keys プロパティのページへのリンク