HtmlForm Class
Provides programmatic access to the HTML <form> element on the server.
For a list of all members of this type, see HtmlForm Members.
System.Object
System.Web.UI.Control
System.Web.UI.HtmlControls.HtmlControl
System.Web.UI.HtmlControls.HtmlContainerControl
System.Web.UI.HtmlControls.HtmlForm
[Visual Basic] Public Class HtmlForm Inherits HtmlContainerControl [C#] public class HtmlForm : HtmlContainerControl [C++] public __gc class HtmlForm : public HtmlContainerControl [JScript] public class HtmlForm extends HtmlContainerControl
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
The HtmlForm control is a container for server controls on a Web Forms page. All server controls that post back to the server must be placed between the opening and closing tags of an HtmlForm control.
Note ASP.NET allows only a single HtmlForm control to be placed on a Web Forms page. If you attempt to place more than one HtmlForm control a Web Forms page, the runtime will throw an System.Web.HttpException when you request the page.
You can control the behavior of the HtmlForm control by setting its properties. To specify the encoding type for the form's data, set the Enctype property. The method (Get or Post) that a browser uses to post form data to the server for processing is specified by setting the Method property.
CAUTION By default, the Method property is set to Post. You can modify the value of this property to Get, but this may break the built-in state and post back services provided by the ASP.NET page framework.
For a list of initial property values for an instance of HtmlForm, see the HtmlForm constructor.
Example
[Visual Basic, C#] The following example demonstrates how to use the HtmlForm class to create a simple form.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Protected Sub AddButton_Click(sender As Object, e As EventArgs) Dim Answer As Integer Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value) AnswerMessage.InnerHtml = Answer.ToString() End Sub </script> </head> <body> <form Method="Post" Enctype="application/x-www-form-urlencoded" runat="server"> <h3> HtmlInputButton Example </h3> <table> <tr> <td colspan="5"> Enter integer values into the text boxes. <br> Click the Add button to add the two values. <br> Click the Reset button to reset the text boxes. </td> </tr> <tr> <td colspan="5"> </td> </tr> <tr align="center"> <td> <input ID="Value1" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> + </td> <td> <input ID="Value2" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> = </td> <td> <span ID="AnswerMessage" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MinCompareValidator" ControlToValidate="Value1" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MaxCompareValidator" ControlToValidate="Value1" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td colspan="2"> <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MinCompareValidator" ControlToValidate="Value2" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MaxCompareValidator" ControlToValidate="Value2" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td>   </td </tr> <tr align="center"> <td colspan="4"> <input Type="Submit" Name="AddButton Value="Add" OnServerClick="AddButton_Click" runat="server"/> <input Type="Reset" Name="AddButton Value="Reset" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> protected void AddButton_Click(Object sender, EventArgs e) { int Answer; Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value); AnswerMessage.InnerHtml = Answer.ToString(); } </script> </head> <body> <form Method="Post" Enctype="application/x-www-form-urlencoded" runat="server"> <h3> HtmlInputButton Example </h3> <table> <tr> <td colspan="5"> Enter integer values into the text boxes. <br> Click the Add button to add the two values. <br> Click the Reset button to reset the text boxes. </td> </tr> <tr> <td colspan="5"> </td> </tr> <tr align="center"> <td> <input ID="Value1" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> + </td> <td> <input ID="Value2" Type="Text" Size="2" MaxLength="3" Value="1" runat="server"/> </td> <td> = </td> <td> <span ID="AnswerMessage" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:RequiredFieldValidator ID="Value1RequiredValidator" ControlToValidate="Value1" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MinCompareValidator" ControlToValidate="Value1" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value1MaxCompareValidator" ControlToValidate="Value1" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td colspan="2"> <asp:RequiredFieldValidator ID="Value2RequiredValidator" ControlToValidate="Value2" ErrorMessage="Please enter an value.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MinCompareValidator" ControlToValidate="Value2" Operator="LessThan" Type="Integer" ValueToCompare="100" ErrorMessage="Please enter an integer less than 100.<br>" Display="Dynamic" runat="server"/> <asp:CompareValidator ID="Value2MaxCompareValidator" ControlToValidate="Value2" Operator="GreaterThan" Type="Integer" ValueToCompare="0" ErrorMessage="Please enter an integer greater than 0.<br>" Display="Dynamic" runat="server"/> </td> <td>   </td </tr> <tr align="center"> <td colspan="4"> <input Type="Submit" Name="AddButton Value="Add" OnServerClick="AddButton_Click" runat="server"/> <input Type="Reset" Name="AddButton Value="Reset" runat="server"/> </td> <td> </td> </tr> </table> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Web.UI.HtmlControls
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
HtmlForm Members | System.Web.UI.HtmlControls Namespace | HtmlContainerControl | Enctype | Method