FormParameter クラス
アセンブリ: System.Web (system.web.dll 内)


FormParameter クラスを使用して、Form コレクションのフォーム変数の値を、パラメータ付きのクエリまたはコマンドで使用するパラメータにバインドできます。FormParameter が指定されていても、対応する変数が渡されないと、データをパラメータにバインドするコントロールは例外をスローします。また、対応する値が渡されずに変数が渡された場合も、データは表示されません。このような状況を防ぐには、必要に応じて DefaultValue を設定します。
FormParameter クラスには、Parameter クラスから継承されたプロパティに加え、バインド先のフォーム変数の名前を識別する FormField プロパティが用意されています。
![]() |
---|
FormParameter は、フォーム要素によって渡された値は一切検証せず、そのままの値を使用します。多くの場合、データ ソース コントロールで発生した Selecting、Updating、Inserting、Deleting などのイベントを処理することによって、FormParameter の値をデータ ソース コントロールで使用される前に検証できます。パラメータの値が検証に合格しなかった場合は、関連付けられた CancelEventArgs クラスの Cancel プロパティを true に設定することによって、データ操作をキャンセルできます。 |

SqlDataSource コントロールと簡単な Web フォーム ページを使用してデータベースにデータを挿入する方法を次のコード例に示します。データ テーブル内の現在のデータが、DropDownList コントロールに表示されます。TextBox コントロールに値を入力し、ボタンをクリックすることにより、新しいレコードを追加できます。ボタンがクリックされると、指定された値がデータベースに挿入され、DropDownList が更新されます。
<%@Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Script runat="server"> Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs) SqlDataSource1.Insert() End Sub ' InsertShipper </Script> <HTML> <BODY> <FORM runat="server"> <asp:dropdownlist id="DropDownList1" runat="server" datasourceid="SqlDataSource1" datatextfield="CompanyName" datavaluefield="ShipperID" /> <!-- Security Note: The SqlDataSource uses a FormParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the FormParameter, handle the Inserting event. --> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind %>" selectcommand="SELECT CompanyName,ShipperID FROM Shippers" insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName,@Phone)"> <insertparameters> <asp:formparameter name="CoName" formfield="CompanyNameBox" /> <asp:formparameter name="Phone" formfield="PhoneBox" /> </insertparameters> </asp:sqldatasource> <p><asp:textbox id="CompanyNameBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyNameBox" Display="Static" ErrorMessage="Please enter a company name." /> <p><asp:textbox id="PhoneBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="PhoneBox" Display="Static" ErrorMessage="Please enter a phone number." /> <p><asp:button id="Button1" runat="server" text="Insert New Shipper" onclick="InsertShipper" /> </FORM> </BODY> </HTML>
<%@Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Script runat="server"> private void InsertShipper (object source, EventArgs e) { SqlDataSource1.Insert(); } </Script> <HTML> <BODY> <FORM runat="server"> <asp:dropdownlist id="DropDownList1" runat="server" datasourceid="SqlDataSource1" datatextfield="CompanyName" datavaluefield="ShipperID" /> <!-- Security Note: The SqlDataSource uses a FormParameter, Security Note: which does not perform validation of input from the client. Security Note: To validate the value of the FormParameter, handle the Inserting event. --> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind %>" selectcommand="SELECT CompanyName,ShipperID FROM Shippers" insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName ,@Phone)"> <insertparameters> <asp:formparameter name="CoName" formfield="CompanyNameBox" /> <asp:formparameter name="Phone" formfield="PhoneBox" /> </insertparameters> </asp:sqldatasource> <p><asp:textbox id="CompanyNameBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyNameBox" Display="Static" ErrorMessage="Please enter a company name." /> <p><asp:textbox id="PhoneBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="PhoneBox" Display="Static" ErrorMessage="Please enter a phone number." /> <p><asp:button id="Button1" runat="server" text="Insert New Shipper" onclick="InsertShipper" /> </FORM> </BODY> </HTML>
<%@Page Language="VJ#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <Script runat="server"> private void InsertShipper(Object source, System.EventArgs e) { SqlDataSource1.Insert(); } //InsertShipper </Script> <HTML> <BODY> <FORM runat="server"> <asp:dropdownlist id="DropDownList1" runat="server" datasourceid="SqlDataSource1" datatextfield="CompanyName" datavaluefield="ShipperID" /> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;Connect Timeout=15" selectcommand="SELECT CompanyName,ShipperID FROM Shippers" insertcommand="INSERT INTO Shippers (CompanyName,Phone) VALUES (@CoName ,@Phone)"> <insertparameters> <asp:formparameter name="CoName" formfield="CompanyNameBox" /> <asp:formparameter name="Phone" formfield="PhoneBox" /> </insertparameters> </asp:sqldatasource> <p><asp:textbox id="CompanyNameBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ControlToValidate="CompanyNameBox" Display="Static" ErrorMessage="Please enter a company name." /> <p><asp:textbox id="PhoneBox" runat="server" /> <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ControlToValidate="PhoneBox" Display="Static" ErrorMessage="Please enter a phone number." /> <p><asp:button id="Button1" runat="server" text="Insert New Shipper" onclick="InsertShipper" /> </FORM> </BODY> </HTML>

System.Web.UI.WebControls.Parameter
System.Web.UI.WebControls.FormParameter


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


- FormParameter クラスのページへのリンク