FormView.InsertItem メソッド
アセンブリ: System.Web (system.web.dll 内)



FormView コントロールが挿入モードの場合に、InsertItem メソッドを使用して、プログラムによって現在のレコードをデータ ソースに挿入します。このメソッドは通常、FormView コントロール以外から (ページ上の別のコントロールなどから) 現在のレコードを挿入する必要がある場合に使用されます。
![]() |
---|
このメソッドが呼び出されたときに FormView コントロールが挿入モードになっている必要があります。そうしないと、HttpException がスローされます。 |
挿入操作の前にページの検証を実行するかどうかを指定するには、causesValidation パラメータを使用します。また、このメソッドを呼び出すと、ItemInserted イベントと ItemInserting イベントも発生します。

InsertItem メソッドを使用して、プログラムによって FormView コントロールの現在のレコードをデータ ソースに挿入する方法を次の例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub InsertButton_Click(ByVal sender As Object, ByVal e As EventArgs) Try ' Use the InsertItem method to programmatically insert ' the current record in the FormView control. EmployeeFormView.InsertItem(True) MessageLabel.Text = "" Catch ex As HttpException MessageLabel.Text = "The FormView control must be in insert mode to insert a record." End Try End Sub Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs) ' Return the FormView control to read-only ' mode. EmployeeFormView.ChangeMode(FormViewMode.ReadOnly) MessageLabel.Text = "" End Sub </script> <html> <body> <form runat="server"> <h3>FormView InsertItem Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." 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> </table> </insertitemtemplate> </asp:formview> <hr/> <asp:Button id="InsertButton" text="Insert Record" onclick="InsertButton_Click" runat="server"/> <asp:Button id="CancelButton" text="Cancel" onclick="CancelButton_Click" runat="server"/> <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 InsertButton_Click(Object sender, EventArgs e) { try { // Use the InsertItem method to programmatically insert // the current record in the FormView control. EmployeeFormView.InsertItem(true); MessageLabel.Text = ""; } catch (HttpException ex) { MessageLabel.Text = "The FormView control must be in insert mode to insert a record."; } } void CancelButton_Click(Object sender, EventArgs e) { // Return the FormView control to read-only // mode. EmployeeFormView.ChangeMode(FormViewMode.ReadOnly); MessageLabel.Text = ""; } </script> <html> <body> <form runat="server"> <h3>FormView InsertItem Example</h3> <asp:formview id="EmployeeFormView" datasourceid="EmployeeSource" allowpaging="true" datakeynames="EmployeeID" emptydatatext="No employees found." 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> </table> </insertitemtemplate> </asp:formview> <hr/> <asp:Button id="InsertButton" text="Insert Record" onclick="InsertButton_Click" runat="server"/> <asp:Button id="CancelButton" text="Cancel" onclick="CancelButton_Click" runat="server"/> <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.InsertItem メソッドを検索する場合は、下記のリンクをクリックしてください。

- FormView.InsertItem メソッドのページへのリンク