asp.net form runat server

b0x0rz picture b0x0rz · Nov 14, 2009 · Viewed 7.9k times · Source

i have a search form on every page, hence i put it on the master page. after adding runat=server to that form i am now unable to use other forms that runat server ;)

how do people usually get around this?

one idea is to surround the whole page with one form runat=server but then i have to have the code in the master page even for those forms that are used on only one sub page (like contact for example).

how do people usually get around this?

thnx

Answer

Abel picture Abel · Nov 14, 2009

This has often been considered one of the most problematic design decisions on ASP.NET. It's a sad thing that you're kinda fixed to this one-form-per-page principle.

One reason that you may want to have multiple forms is that the default button will be set by the browser to the submit button. Someone typing into your search box and hitting enter should not submit the main form (suppose that's the login page), but the tiny form of the search button.

The easiest and "standard" way to work around this is using an asp:Panel and set the DefaultButton property. This should wrap around the part that includes both the search fields and the search button.

Don't worry about coding the <form> from inside the masterpage, surrounding the whole page. That's a common design. Most server controls require a form, so why not simply include it regardless: it is supposed to be there.