HtmlForm Control

Creates a server-side control that maps to the <form> HTML element and allows you to create a container for elements in a Web page.

<form id="programmaticID"
      method=POST | GET
      action="srcpageURL"
      runat="server" >
   Other controls, input forms, and so on.
</form>

Remarks

Use the HtmlForm control to program against the HTML <form> element. To take advantage of the postback services, all Web Forms controls, whether HTML, Web, pagelet, or custom, must be nested between well-formed opening and closing tags of the HtmlForm control. If the tags are not closed properly, ASP.NET will not recognize the element. The element will either be ignored or a compilation error will occur, depending on how the element is formed.

Note   You cannot include more than one HtmlForm control on a single Web Forms page.

By default, the HtmlForm control's method attribute is set to POST. You can customize the method attribute to suit your needs, but setting the method attribute to a value other than GET or POST can break the built-in view state and post back services provided by the Web Forms.

Note   The action attribute is always set to the URL of the page itself. The action attribute cannot be changed; therefore, you can only post back to the page itself.

Example

The following example shows three HtmlButton controls with a separate OnServerClick handler for each button. Each of these events causes a post back to the server (the HtmlForm control is required for any scenario in which a post back occurs). This example also demonstrates that only one HtmlForm control is allowed on a Web Forms page, including a form that supports multiple events. If you include more than one HtmlForm control, the .NET Framework will throw an exception.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Button1_OnClick(Source As Object, e As EventArgs)
         Span1.InnerHtml = "You clicked Button1"
      End Sub

      Sub Button2_OnClick(Source As Object, e As EventArgs)
         Span2.InnerHtml = "You clicked Button2"
      End Sub

      Sub Button3_OnClick(Source As Object, e As EventArgs)
         Span3.InnerHtml = "You clicked Button3"
      End Sub
   </script>
</head>
<body>
   <h3>HtmlForm Sample</h3>
   <form id="ServerForm" runat="server">     
      <button id=Button1 runat="server"
              OnServerClick="Button1_OnClick">
         Button1
      </button>
      &nbsp;&nbsp;
      <span id=Span1 runat="server" />
      <p>
      <button id=Button2 runat="server"
              OnServerClick="Button2_OnClick">
         Button2
      </button>
      &nbsp;&nbsp;
      <span id=Span2 runat="server" />
      <p>
      <button id=Button3 runat="server"
              OnServerClick="Button3_OnClick">
         Button3
      </button>
      &nbsp;&nbsp;
      <span id=Span3 runat="server" />
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
    <script runat="server">
       void Button1_OnClick(object Source, EventArgs e) 
       {
          Span1.InnerHtml="You clicked Button1";
       }
       void Button2_OnClick(object Source, EventArgs e) 
       {
          Span2.InnerHtml="You clicked Button2";
       }
       void Button3_OnClick(object Source, EventArgs e) 
       {
          Span3.InnerHtml="You clicked Button3";
       }
    </script>
</head>
<body>
   <h3>HtmlForm Sample</h3>
   <form id="ServerForm" runat="server">     
      <button id=Button1 runat="server"
              OnServerClick="Button1_OnClick">
         Button1
      </button>
      &nbsp;&nbsp;
      <span id=Span1 runat="server" />
      <p>
      <button id=Button2 runat="server"
              OnServerClick="Button2_OnClick">
         Button2
      </button>
      &nbsp;&nbsp;
      <span id=Span2 runat="server" />
      <p>
      <button id=Button3 runat="server"
              OnServerClick="Button3_OnClick">
         Button3
      </button>
      &nbsp;&nbsp;
      <span id=Span3 runat="server" />
   </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlForm Class