Home > Getting Started > ASP.NET Syntax > ASP.NET Syntax for HTML Controls > HtmlForm
ASP.NET Syntax ASP.NET Syntax for HTML Controls
Creates an HTML form control, which acts as a container for user input controls.
Declarative Syntax
<form id="accessID" runat="server"
method = post | get >
... user input controls ...
</form>
For information on the individual members of this class, see HtmlForm in the class library.
- 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.
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 view state and postback services provided by the Web Forms Page Framework.
The following sample illustrates using an HtmlForm control that
contains a set of HtmlButton controls. The form handler demonstrates a
simple way to determine which of the form’s controls have been selected, using
the individual control’s ID.
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 = "/asp.net.ph/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>
Remember that only one HtmlForm control is allowed on a Web Forms page, even for a form that supports multiple events. If you include more than one HtmlForm control, the compiler will throw an exception.
Web Forms Events and Handlers HtmlForm Class