简体   繁体   中英

asp.net form runat server

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

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.

I'd suggest you to have search html form w/o runat=server on the master page, like

<form action="Search.aspx" method="get">
  <input type="text" name="q" /><input type="submit" />
</form>

Use method=get to have possibility take URL of the search. And then do whatever you need with the query in codebehind of the Search.aspx:

public void Page_Load(object sender)
{
  string q = Context.Request.QueryString["q"];
  /*...*/
}

First question is, why do you want to have multiple runat="server" forms?

You can still have forms without runat="server" on the page, or use the PostBackUrl property on Button controls, to post to different ASP.NET pages.

To collect data in a multi-step process, you can either look at the ASP.NET Wizard control, see Scott Guthrie's blog post here .

Alternatively you can manage it all yourself and use ASP.NET Panels, or use the already mentioned PostBackUrl to navigate from ASP.NET page to a different page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM