Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Using Multiple Forms in ASP.NET


Using Multiple Forms in ASP.NET

How can I use multiple HTML forms in an ASP.NET application? How can I build a page to make it contain a small form in a corner for users to login or start a search? These are frequently asked questions from those who do serious ASP.NET development. The problem arises from the fact that ASP.NET forces a single-form programming model. In HTML, the form is the construct designed to gather some input data and post it to the server for further processing. The form is seen as a collection of <input> tags such as textboxes, drop-down lists, and checkboxes, whose contents are automatically collected and posted when a submit button is pressed. Likewise, the same finalizing process can be started via script. In HTML, you can have as many forms as you like in each and every HTML page. This structure has been maintained in Active Server Pages (ASP), mostly because ASP is a sort of wrapper built around the HTML page. In the end, with ASP you create the page structure from an HTML basement with some opened windows of dynamically generated markup.

ASP.NET is quite different and qualifies as a true runtime environment that enforces its own programming rules to generate the same kind of output—HTML markup. In doing so, ASP.NET mandates that, at most, one form is supported, and any submissions the web server can receive from a page should target the same original URL. In short, each ASP.NET page can contain no more than one HTML form and can post only to itself.

There are many benefits to the ASP.NET programming model out of this pattern, the most important of which is the ability to handle postback events on top of a stateful architecture. In ASP, how did you manage to maintain the state of input elements across two successive requests for the same page? Should I say it? I guess you were forcing the action attribute of the HTML form to the same URL of the current page and using some code blocks and the session state to restore key attributes on input fields. At its core, this is just what ASP.NET does, except that it uses viewstate instead of session state and that it promotes the model to rank of a “mandatory” rather than “optional” feature.

In summary, to be able to use ASP.NET server controls for capturing some user input, you have to use the ASP.NET server form control. In doing so, you lose the power of controlling the action attribute of the form and are limited to, at most, one form per page.

Hence the initial question, how can you insert a second form in an ASP.NET page?

If your goal is “changing” the form from time to time, obeying different runtime conditions, then be aware that the enforced rule states that only one form can be visible at any time. So you can place as many as <form runat=server> tags as needed in a page, as long as only one of them is marked with visible=true. By acting on the visible attribute, you can programmatically switch from one form to the next.

If your goal is having a “child” form for login or search purposes, the solution is even simpler: Stick to classic HTML forms; that is, <form> tags devoid of the runat attribute. ASP.NET doesn’t exercise any control on tags not marked with the runat attribute and you’re just fine until HTML supports multiple forms. What do you lose in the change? The ability of using postbacks in the target page of the login or search. Just use the ASP classic programming model—the Request object—and you’re all set.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.