asp.net.ph

Skip Navigation LinksHome > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlForm

HtmlForm Control Syntax

HtmlForm Class   Form Element


Creates an HTML form control, which acts as a container for user input controls.

Declarative Syntax

For information on the individual members of this class, see HtmlForm in the class library.

Syntax Notes

  • To take advantage of the page framework’s postback services, all Web Forms controls - whether HTML, Web, user or custom - must be nested within the HtmlForm control.
  • You cannot include more than one HtmlForm control on a single Web Forms page.

Working with HtmlForm

The HtmlForm control enables programming of the HTML <form> element.

An HtmlForm control is required whenever user input needs to be sent for processing on a Web server.

A Web Form may only have one server-side <form> tag. However, client forms ( no runat="server" attribute ) can also postback to server-side logic as long as a server-side HtmlForm is present on the page.

By default, the HtmlForm control’s method attribute is set to POST, and the form is set to post back to the URL of the source page ( to the same page where the HtmlForm is declared ).

You can modify this to suit your needs, but doing so can break the built-in viewstate and postback services provided by the Web Forms Page Framework.

Using the HtmlForm Control for Postback Requests

The following illustrates using an HtmlForm control that contains a set of HtmlButton controls.

Using the HtmlForm Control for Postback Requests
Run Sample | View Source

The below code shows the markup for:

  • the buttons, the click events of which are all set to invoke a common handler;
  • an <asp:AdRotator> control, which randomly selects a book filtered by subject based on the ID of the selected button;
  • and an <asp:Label> control, which renders a message based on the InnerText of the selected button.
<form runat="server">

   <table>
   <tr>
      <td>
         <button id="aspnet" runat="server" onServerClick = "getBook">ASP.NET</button>
         <button id="adonet" runat="server" onServerClick = "getBook">ADO.NET</button>
         <button id="csharp" runat="server" onServerClick = "getBook">C#</button>
         <button id="vbnet" runat="server" onServerClick = "getBook">VB.NET</button>
         <button id="xml" runat="server" onServerClick = "getBook">XML</button>
      </td>
   </tr>
   </table>

   <p><asp:adrotator id="book"
      advertisementfile="~/shared/books.xml"
      bordercolor="silver" borderwidth=1
      target="_blank" runat="server" />

   
   <p><asp:label id="msg" runat="server"
      text="Click on any of the above buttons to check out other books." />

</form>

Each of the buttons’ click event causes a postback to the server, which triggers the same handler. The below shows the method for handling the button events.

<script language="C#" runat="server">
   void getBook ( Object sender, EventArgs e ) {
      string id = ( ( HtmlButton ) sender ).ID;
      book.KeywordFilter = id;
      book.Visible = true;

      string txt = ( ( HtmlButton ) sender ).InnerText= = "C#" ? 
         "C sharp" : ( ( HtmlButton ) sender ).InnerText;
      msg.Text = "Check out other " + ( ( HtmlButton ) sender ).InnerText + 
         " books at Amazon";
   }
</script>

The sender argument above refers to the button that raised the event and invoked the handler method.

The common handler for each buton first determines which of the buttons have been selected, then sets the KeywordFilter filter of the AdRotator control based on the sender’s ID, then sets the Text property of the Label control based on the sender’s InnerText property.

See Also

HtmlForm Class   Form Element   Web Forms Events and Handlers



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note