Compartilhar via


HtmlForm Server Control Declarative Syntax 

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 
    DefaultButton="string"
    DefaultFocus="string"
    EnableViewState="False|True"
    Id="string"
    SubmitDisabledControls="False|True"
    Visible="True|False"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnUnload="OnUnload event handler"
    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 postback services, all Web Forms controls, whether HTML controls, Web controls, user controls, or custom controls, 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. Either the element will be ignored or a compilation error will occur, depending on how the element is formed.

NoteNote

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 postback services provided by ASP.NET.

NoteNote

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 postback to the server (the HtmlForm control is required for any scenario in which a postback 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlForm Control</title>

   <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>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>HtmlForm Control</title>

    <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

Reference

HtmlForm

Other Resources

HTML Server Controls