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

更新するレコードの元のキー以外のフィールドの名前と値のペアのディクショナリが格納されている IOrderedDictionary。

OldValues プロパティを使用して、更新されたレコードのキー以外の元のフィールド値にアクセスします。たとえば、これらの値を使用して、更新されたレコードのログを維持できます。SqlDataSource コントロールや ObjectDataSource コントロールなど、一部のデータ ソースは、競合の検出 (オプティミスティック同時実行制御のチェック) をサポートしています。これらのいずれかのコントロールの ConflictDetection プロパティが ConflictOptions.CompareAllValues に設定されている場合、更新操作の実行前に、レコードの元の値とデータ ソース内の値が比較されます。レコードが同時に別のユーザーによって変更された場合、そのレコードは更新されません。
![]() |
---|
OldValues プロパティには、キー フィールドは格納されません。ユーザーがキー フィールドの値を更新できるようにしている場合、元のキー フィールドの値は Keys プロパティに格納されます。更新された値は、NewValues プロパティに格納されます。 |
NewValues プロパティは、System.Collections.Specialized.IOrderedDictionary インターフェイスを実装する OrderedDictionary オブジェクトを返します。OrderedDictionary オブジェクトは、更新されたレコードのフィールドを表す System.Collections.DictionaryEntry オブジェクトを格納します。フィールド名にアクセスするには、OrderedDictionary オブジェクトの Keys プロパティを使用します。同様に、Values プロパティを使用して、フィールド値にアクセスできます。

OldValues プロパティを使用して、更新中のレコードのキー以外のフィールドの元の値にアクセスする方法を次の例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub EmployeeFormView_ItemUpdating(ByVal sender As Object, ByVal e As FormViewUpdateEventArgs) Handles EmployeeFormView.ItemUpdating ' Validate the field values entered by the user. This ' example determines whether the user left any fields ' empty. The names of the empty fields are added to ' an ArrayList object. ' Use the NewValues property to access the new ' values entered by the user. Dim emptyFieldList As ArrayList = ValidateFields(CType(e.NewValues, OrderedDictionary)) 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("EmployeeID").ToString() MessageLabel.Text = "You must enter a value for each field 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 OrderedDictionary) 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. Dim entry As DictionaryEntry For Each entry In list If entry.Value.Equals("") Then ' Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()) End If Next Return emptyFieldList End Function Sub EmployeeFormView_ModeChanged(ByVal sender As Object, ByVal e As EventArgs) Handles EmployeeFormView.ModeChanged ' Clear the error message label. MessageLabel.Text = "" End Sub </script> <html> <body> <form runat="server"> <h3>FormViewUpdateEventArgs Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." runat="server"> <itemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <%# Eval("FirstName") %> <%# Eval("LastName") %> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <%# Eval("Title") %> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <%# Eval("Address") %><br/> <%# Eval("City") %> <%# Eval("Region") %> <%# Eval("PostalCode") %><br/> <%# Eval("Country") %> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="Edit" text="Edit" commandname="Edit" runat="server"/> </td> </tr> </table> </itemtemplate> <edititemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeEditImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameUpdateTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameUpdateTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleUpdateTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <asp:textbox id="AddressUpdateTextBox" text='<%# Bind("Address") %>' runat="server"/> <br/> <asp:textbox id="CityUpdateTextBox" text='<%# Bind("City") %>' runat="server"/> <asp:textbox id="RegionUpdateTextBox" text='<%# Bind("Region") %>' width="40" runat="server"/> <asp:textbox id="PostalCodeUpdateTextBox" text='<%# Bind("PostalCode") %>' width="60" runat="server"/> <br/> <asp:textbox id="CountryUpdateTextBox" text='<%# Bind("Country") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </edititemtemplate> </asp:formview> <br/><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="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]" updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void EmployeeFormView_ItemUpdating(Object sender, FormViewUpdateEventArgs e) { // Validate the field values entered by the user. This // example determines whether the user left any fields // empty. The names of the empty fields are added to // an ArrayList object. // Use the NewValues property to access the new // values entered by the user. ArrayList emptyFieldList = ValidateFields((OrderedDictionary)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["EmployeeID"].ToString(); MessageLabel.Text = "You must enter a value for each field of record " + keyValue + ".<br/>The following fields are missing:<br/><br/>"; // Display the missing fields. foreach (String value in emptyFieldList) { // Use the OldValues property to 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(OrderedDictionary 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. foreach (DictionaryEntry entry in list) { if (entry.Value.Equals("")) { // Add the field name to the ArrayList object. emptyFieldList.Add(entry.Key.ToString()); } } return emptyFieldList; } void EmployeeFormView_ModeChanged(Object sender, EventArgs e) { // Clear the error message label. MessageLabel.Text = ""; } </script> <html> <body> <form runat="server"> <h3>FormViewUpdateEventArgs Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." onitemupdating="EmployeeFormView_ItemUpdating" onmodechanged="EmployeeFormView_ModeChanged" runat="server"> <itemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <%# Eval("FirstName") %> <%# Eval("LastName") %> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <%# Eval("Title") %> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <%# Eval("Address") %><br/> <%# Eval("City") %> <%# Eval("Region") %> <%# Eval("PostalCode") %><br/> <%# Eval("Country") %> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="Edit" text="Edit" commandname="Edit" runat="server"/> </td> </tr> </table> </itemtemplate> <edititemtemplate> <table> <tr> <td rowspan="6"> <asp:image id="EmployeeEditImage" imageurl='<%# Eval("PhotoPath") %>' alternatetext='<%# Eval("LastName") %>' runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameUpdateTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameUpdateTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleUpdateTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr height="150" valign="top"> <td> <b>Address:</b> </td> <td> <asp:textbox id="AddressUpdateTextBox" text='<%# Bind("Address") %>' runat="server"/> <br/> <asp:textbox id="CityUpdateTextBox" text='<%# Bind("City") %>' runat="server"/> <asp:textbox id="RegionUpdateTextBox" text='<%# Bind("Region") %>' width="40" runat="server"/> <asp:textbox id="PostalCodeUpdateTextBox" text='<%# Bind("PostalCode") %>' width="60" runat="server"/> <br/> <asp:textbox id="CountryUpdateTextBox" text='<%# Bind("Country") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="UpdateButton" text="Update" commandname="Update" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </edititemtemplate> </asp:formview> <br/><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="EmployeeSource" selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [Address], [City], [Region], [PostalCode], [Country], [HireDate], [PhotoPath] From [Employees]" updatecommand="Update [Employees] Set [LastName]=@LastName, [FirstName]=@FirstName, [Title]=@Title, [Address]=@Address, [City]=@City, [Region]=@Region, [PostalCode]=@PostalCode, [Country]=@Country Where [EmployeeID]=@EmployeeID" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


FormViewUpdateEventArgs クラス
FormViewUpdateEventArgs メンバ
System.Web.UI.WebControls 名前空間
FormView クラス
FormViewUpdateEventHandler
System.Collections.Specialized.IOrderedDictionary
System.Collections.Specialized.OrderedDictionary
System.Collections.DictionaryEntry
OrderedDictionary.Keys
OrderedDictionary.Values
FormViewUpdateEventArgs.Keys プロパティ
FormViewUpdateEventArgs.OldValues プロパティ
FormView.ItemUpdating イベント
Weblioに収録されているすべての辞書からFormViewUpdateEventArgs.OldValues プロパティを検索する場合は、下記のリンクをクリックしてください。

- FormViewUpdateEventArgs.OldValues プロパティのページへのリンク