Using multiple or nested forms in ASP.NET

While the convenience of having just one form in asp.net is great, there are times when you actually need to have multiple forms such as when you want to give a search functionality on your website or have a form post to another page without getting into the intricacies of PreviousPage and the like.

It is must be noted that asp.net allow just 1 form with the runat attribute which is visible at any given time. This means two things – first you can have multiple forms on the same webpage provided only one of them has the runat attribute or you can have multiple forms with the runat attribute provided only of them is visible at any given time (the others must be invisible so that asp.net renders just one form tag for you).

Nested forms in asp.net

So now that you know that you can have many forms in your asp.net page and have decided to have a search facility at the top and maybe another form to post comments to a different page, it is important to remember that the forms cannot be nested, otherwise it wouldn’t work as expected. So make sure that you’re closing your form tag before opening another one!

Another thing worth mentioning is that when you’re posting to a different page with a simple html form, you need to provide both the ID and Name attribute of the element you wish to retrieve the value of from the other side. So if on Page A you have a textarea, don’t forget the name attribute because this is necessary for Request.Form to retrieve the value of your textarea.

comments powered by Disqus