FormView.ItemInserting イベント
アセンブリ: System.Web (system.web.dll 内)

Dim instance As FormView Dim handler As FormViewInsertEventHandler AddHandler instance.ItemInserting, handler
public: event FormViewInsertEventHandler^ ItemInserting { void add (FormViewInsertEventHandler^ value); void remove (FormViewInsertEventHandler^ value); }

FormView コントロールは、コントロール内の Insert ボタン (CommandName プロパティが "Insert" に設定されたボタン) がクリックされ、FormView コントロールが実際にレコードを挿入する前に ItemInserting イベントを発生させます。これにより、このイベントが発生するたびにカスタム ルーチン (データ ソースに挿入する前にレコードの値を HTML エンコーディングするなど) を実行するイベント処理メソッドを提供できます。
FormViewInsertEventArgs オブジェクトがイベント処理メソッドに渡されることにより、FormView コントロールに送信される省略可能なコマンド引数の値を確認したり、挿入操作をキャンセルする必要があることを示したりできます。コマンド引数の値は、Insert ボタンの CommandArgument プロパティに対応します。Insert ボタンの CommandArgument プロパティが設定されている場合は、CommandArgument プロパティを使用してコマンド引数の値を確認します。挿入操作をキャンセルするには、Cancel プロパティを true に設定します。また、Values プロパティを使用して、新規レコードのフィールド値を読み込んだり、変更したりすることもできます。

ItemInserting イベントを使用して、挿入操作をキャンセルする方法を次の例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub EmployeeFormView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs) MessageLabel.Text = "" ' Iterate through the items in the Values collection ' and verify that the user entered a value for each ' text box displayed in the insert item template. Cancel ' the insert operation if the user left a text box empty. ' In Visual Basic, the DictionaryItem objects contained in ' the Values collection must be copied to an array before ' you can iterate through the collection. Dim itemArray(e.Values.Count - 1) As DictionaryEntry e.Values.CopyTo(itemArray, 0) Dim entry As DictionaryEntry For Each entry In itemArray If entry.Value.Equals("") Then ' Use the Cancel property to cancel the ' insert operation. e.Cancel = True MessageLabel.Text &= "Please enter a value for the " & _ entry.Key.ToString() & " field.<br/>" End If Next End Sub Sub EmployeeFormView_ModeChanged(ByVal sender As Object, ByVal e As EventArgs) ' Clear the MessageLabel Label control when the FormView ' control changes modes. MessageLabel.Text = "" End Sub </script> <html> <body> <form runat="server"> <h3>FormViewInsertEventArgs Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." oniteminserting="EmployeeFormView_ItemInserting" onmodechanged="EmployeeFormView_ModeChanged" runat="server"> <itemtemplate> <table> <tr> <td rowspan="5"> <asp:image id="CompanyLogoImage" imageurl="~/Images/Logo.jpg" alternatetext="Company Logo" 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> <td colspan="2"> <asp:linkbutton id="NewButton" text="New" commandname="New" runat="server"/> </td> </tr> </table> </itemtemplate> <insertitemtemplate> <table> <tr> <td rowspan="4"> <asp:image id="CompanyLogoEditImage" imageurl="~/Images/Logo.jpg" alternatetext="Company Logo" runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameInsertTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameInsertTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleInsertTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="InsertButton" text="Insert" commandname="Insert" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </insertitemtemplate> </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], [PhotoPath] From [Employees]" insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void EmployeeFormView_ItemInserting(Object sender, FormViewInsertEventArgs e) { MessageLabel.Text = ""; // Iterate through the items in the Values collection // and verify that the user entered a value for each // text box displayed in the insert item template. Cancel // the insert operation if the user left a text box empty. foreach (DictionaryEntry entry in e.Values) { if (entry.Value.Equals("")) { // Use the Cancel property to cancel the // insert operation. e.Cancel = true; MessageLabel.Text += "Please enter a value for the " + entry.Key.ToString() + " field.<br/>"; } } } void EmployeeFormView_ModeChanged(Object sender, EventArgs e) { // Clear the MessageLabel Label control when the FormView // control changes modes. MessageLabel.Text = ""; } </script> <html> <body> <form runat="server"> <h3>FormViewInsertEventArgs Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." oniteminserting="EmployeeFormView_ItemInserting" onmodechanged="EmployeeFormView_ModeChanged" runat="server"> <itemtemplate> <table> <tr> <td rowspan="5"> <asp:image id="CompanyLogoImage" imageurl="~/Images/Logo.jpg" alternatetext="Company Logo" 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> <td colspan="2"> <asp:linkbutton id="NewButton" text="New" commandname="New" runat="server"/> </td> </tr> </table> </itemtemplate> <insertitemtemplate> <table> <tr> <td rowspan="4"> <asp:image id="CompanyLogoEditImage" imageurl="~/Images/Logo.jpg" alternatetext="Company Logo" runat="server"/> </td> <td colspan="2"> </td> </tr> <tr> <td> <b>Name:</b> </td> <td> <asp:textbox id="FirstNameInsertTextBox" text='<%# Bind("FirstName") %>' runat="server"/> <asp:textbox id="LastNameInsertTextBox" text='<%# Bind("LastName") %>' runat="server"/> </td> </tr> <tr> <td> <b>Title:</b> </td> <td> <asp:textbox id="TitleInsertTextBox" text='<%# Bind("Title") %>' runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:linkbutton id="InsertButton" text="Insert" commandname="Insert" runat="server"/> <asp:linkbutton id="CancelButton" text="Cancel" commandname="Cancel" runat="server"/> </td> </tr> </table> </insertitemtemplate> </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], [PhotoPath] From [Employees]" insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)" 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からFormView.ItemInserting イベントを検索する場合は、下記のリンクをクリックしてください。

- FormView.ItemInserting イベントのページへのリンク