HtmlForm Class
Assembly: System.Web (in system.web.dll)
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 active on a Web Forms page. If there is more than one active HtmlForm control on a Web Forms page, the common language runtime will throw an System.Web.HttpException exception when you request the page. You can, however, use a MultiView control where each View object contains one HtmlForm control, because only one View is active at any given time. |
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 might break the built-in state and postback services provided by the ASP.NET page framework. |
You can configure controls that implement the IButtonControl interface to post to a different target page. This is referred to as cross-page posting. For more information, see Cross-Page Posting in ASP.NET Web Pages.
For a list of initial property values for an instance of HtmlForm, see the HtmlForm constructor.
The following code example demonstrates how to use the HtmlForm class to create a simple form.
<%@ Page Language="C#" AutoEventWireup="True" %> <script runat="server"> protected void AddButton_Click(Object sender, EventArgs e) { int Answer; // Calculate and display the result. Answer = Convert.ToInt32(Value1.Value) + Convert.ToInt32(Value2.Value); AnswerMessage.InnerHtml = Answer.ToString(); } </script> <html> <head> <title>HtmlForm Example</title> </head> <body> <form Method="Post" Enctype="application/x-www-form-urlencoded" runat="server" id="myform"> <h3> HtmlForm 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 a 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 a 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>
- AspNetHostingPermission for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
- AspNetHostingPermission for operating in a hosted environment. Demand value: InheritanceDemand; Permission value: Minimal.
System.Web.UI.Control
System.Web.UI.HtmlControls.HtmlControl
System.Web.UI.HtmlControls.HtmlContainerControl
System.Web.UI.HtmlControls.HtmlForm
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
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Note
Caution